Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 共同偏好&;旋转器未保持状态_Android_Sharedpreferences_Android Spinner - Fatal编程技术网

Android 共同偏好&;旋转器未保持状态

Android 共同偏好&;旋转器未保持状态,android,sharedpreferences,android-spinner,Android,Sharedpreferences,Android Spinner,我有一个这样的旋转器: // Spinner 1 final Spinner plan = (Spinner) dialog.findViewById(R.id.spinner1); strings = getResources().getStringArray(R.array.paymentplan); sAdapter = new SpinnerAdapter(this, android.R.layout.simple_spinner_item, strings); sA

我有一个这样的旋转器:

// Spinner 1


final Spinner plan = (Spinner) dialog.findViewById(R.id.spinner1);
strings = getResources().getStringArray(R.array.paymentplan);
sAdapter = new SpinnerAdapter(this,
        android.R.layout.simple_spinner_item, strings);
sAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
plan.setAdapter(sAdapter);

// plan.setAdapter(spinner1Adapter);
plan.setSelection(prefsDisplay.getInt("spinnerSelection1", 0));
plan.setOnItemSelectedListener(new MyOnItemSelectedListenerPlan());
当用户单击时,我希望它保存状态:

  public void onClick(View v) {
                Editor editor2 = prefsPlan.edit();
                int selectedPosition1 = plan.getSelectedItemPosition();
                editor2.putInt("spinnerSelection1", selectedPosition1);
                editor2.commit();


}

它将位置保存在SharedPref中,但微调器将返回默认值。有人在这里看到什么吗?

您正在存储
喷丝头选择

editor1.putInt("spinnerSelection", selectedPosition);
正在访问的
喷丝头选择1

prefsDisplay.getInt("spinnerSelection1", 0)
使它们一致

更新

当您访问
plan.getSelectedItemPosition()
时。那么微调器是否可见?我想没有


尝试为所选位置放置公共变量。并在您的
MyOnItemSelectedListenerPlan
中更新
所选位置。然后将该位置存储在共享首选项中。我想这能解决你的问题

您正在存储
喷丝头选择

editor1.putInt("spinnerSelection", selectedPosition);
正在访问的
喷丝头选择1

prefsDisplay.getInt("spinnerSelection1", 0)
使它们一致

更新

当您访问
plan.getSelectedItemPosition()
时。那么微调器是否可见?我想没有


尝试为所选位置放置公共变量。并在您的
MyOnItemSelectedListenerPlan
中更新
所选位置。然后将该位置存储在共享首选项中。我想这能解决你的问题

保存到首选项后,必须将所选项目设置为微调器以供进一步使用

作为


但您使用的是
喷丝头选择1
。因此,默认情况下,如果首选项中没有匹配项。默认值将返回。因此这里返回0,并将微调器设置为第一个位置

保存到首选项后,您必须将所选项目设置为微调器以供进一步使用

作为

但您使用的是
喷丝头选择1
。因此,默认情况下,如果首选项中没有匹配项。默认值将返回。因此这里返回0,并将微调器设置为第一个位置

要保存:

要加载:

如果您使用的是数组,它应该这样更改

String selectedString = yourArray[yourSpinner.getSelectedItemPosition()];
editor.putString("spinnerSelection", selectedString);
editor.commit();
如果使用 ArrayList可以通过调用

提交时,显示所有以上数组项已消失。不要让任何人进入 数组

要保存:

要加载:

如果您使用的是数组,它应该这样更改

String selectedString = yourArray[yourSpinner.getSelectedItemPosition()];
editor.putString("spinnerSelection", selectedString);
editor.commit();
如果使用 ArrayList可以通过调用

提交时,显示所有以上数组项已消失。不要让任何人进入 数组


尝试下面的代码,首先使用下面的代码将当前选定项的位置保存到
onItemSelectedListener()
上的一个整数变量中,然后将此变量值存储到共享首选项中

SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putInt("SelectedIndex", index);
prefsEditor.commit();
用于将值存储到一个变量中

int index;

public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
    // Here Position is Item Index
    index = position;
}
有关更多信息,请参阅下面的链接


尝试下面的代码,首先使用下面的代码将当前选定项的位置保存到
onItemSelectedListener()
上的一个整数变量中,然后将此变量值存储到共享首选项中

SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putInt("SelectedIndex", index);
prefsEditor.commit();
用于将值存储到一个变量中

int index;

public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
    // Here Position is Item Index
    index = position;
}
有关更多信息,请参阅下面的链接


但我在这里设置:
plan.setSelection(prefsDisplay.getInt(“spinnerSelection1”,0))另外,我更改了代码,我有错误的代码。喷丝头选择是一致的。当您在日志中设置喷丝头时,尝试打印
喷丝头选择1
,但我在这里设置:
plan.setSelection(prefsDisplay.getInt(“喷丝头选择1”,0))另外,我更改了代码,我有错误的代码。喷丝头选择是一致的。尝试打印
喷丝头选择1
当您在日志中设置喷丝头时,谢谢,我会检查出来的谢谢,我会检查出来的