我有一些旋转器,我设置值,其中四个。我希望他们在启动之间保留当前设置。在微调器中选择一个值时,在按下updateSettings按钮之前不应保存该值。我该怎么做?
这里有一些代码,在onCreate中我使用默认值制作微调器:
mBaudSpinner = (Spinner) findViewById(R.id.bannerBaudSpinner);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
this, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mBaudSpinner.setAdapter(adapter);
String[] tempArray = SlickUSB2Serial.BAUD_RATES;
for (int i = 0; i < tempArray.length; i++) {
adapter.add(tempArray[i]);
}
mBaudSpinner.setSelection(SlickUSB2Serial.BaudRate.BAUD_9600.ordinal());
mDataSpinner = (Spinner) findViewById(R.id.bannerDataSpinner);
adapter = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mDataSpinner.setAdapter(adapter);
tempArray = SlickUSB2Serial.DATA_BITS;
for (int i = 0; i < tempArray.length; i++) {
adapter.add(tempArray[i]);
}
mDataSpinner
.setSelection(SlickUSB2Serial.DataBits.DATA_8_BIT.ordinal());
mParitySpinner = (Spinner) findViewById(R.id.bannerParitySpinner);
adapter = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mParitySpinner.setAdapter(adapter);
tempArray = SlickUSB2Serial.PARITY_OPTIONS;
for (int i = 0; i < tempArray.length; i++) {
adapter.add(tempArray[i]);
}
mParitySpinner.setSelection(SlickUSB2Serial.ParityOption.PARITY_NONE
.ordinal());
mStopSpinner = (Spinner) findViewById(R.id.bannerStopSpinner);
adapter = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mStopSpinner.setAdapter(adapter);
tempArray = SlickUSB2Serial.STOP_BITS;
for (int i = 0; i < tempArray.length; i++) {
adapter.add(tempArray[i]);
}
mStopSpinner
.setSelection(SlickUSB2Serial.StopBits.STOP_1_BIT.ordinal());
如果我点击updateSettings,则运行以下代码:
else if (v == mUpdateSettings) {
if (mSelectedAdapter == null) {
return;
}
mSelectedAdapter.setCommSettings(BaudRate.values()[mBaudSpinner
.getSelectedItemPosition()], DataBits.values()[mDataSpinner
.getSelectedItemPosition()],
ParityOption.values()[mParitySpinner
.getSelectedItemPosition()],
StopBits.values()[mStopSpinner.getSelectedItemPosition()]);
}
我试图以一个微调器为例来使用这样的首选项,它是否在正确的轨道上?
SharedPreferences prefs;
prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor prefEditor = prefs.edit();
prefEditor.putString("savedValue",mBaudSpinner.getSelectedItem().toString());
prefEditor.commit();
String username = prefs.getString("savedValue", String.valueOf(SlickUSB2Serial.BaudRate.BAUD_9600.ordinal()));
mBaudSpinner.setSelection(Integer.parseInt(username));
你做什么,我是否将sharedPrefs的东西设为全局,以便它可以在onCreate和按钮点击监听器中使用?然后将设置微调器默认值的行更改为例如:
String username = prefs.getString("savedValue", String.valueOf(SlickUSB2Serial.BaudRate.BAUD_9600.ordinal()));
mBaudSpinner.setSelection(Integer.parseInt(username));
然后把关于保存值的其他东西放在按钮监听器中?我正在尝试这个,但状态不会保存并始终默认为错误的值。我正在做的是什么?
修改
这些行似乎没有设置默认值,为什么会这样?
prefs = PreferenceManager.getDefaultSharedPreferences(this);
String username = prefs.getString("savedValue", Integer.toString(SlickUSB2Serial.BaudRate.BAUD_9600.ordinal()));
mBaudSpinner.setSelection(Integer.parseInt(username));
答案 0 :(得分:1)
看,当你打电话:
mBaudSpinner.getSelectedItem()
你有一个对象并将其转换为字符串。当你打电话:
mBaudSpinner.setSelection(Integer.parseInt(username))
你得到字符串并转换为int。但是,setSelectionshould
接收一个int位置,getSelectedItem返回一个对象,而不是项目位置。