好的,所以我只想保存,然后从NumberPicker加载数据。 NumberPicker值最初设置为字符串数组,因为需要从NumberPicker中选择特定值setDisplayedValues()方法。
在加载时我没有遇到任何问题。因为我猜我需要给出在数组中找到该值的位置的索引。这就是我一直在尝试但无济于事的
minSpinNP.setValue(Integer.parseInt(spinValues[minSpinPos]));
maxSpinNP.setValue(Integer.parseInt(spinValues[maxSpinPos]));
我不相信numberPicker必须采用int或字符串数组。 加载prefs()
时,我也收到此错误12-12 19:57:34.724: E/AndroidRuntime(7612): Caused by: java.lang.ClassCastException:
java.lang.String cannot be cast to java.lang.Integer
12-12 19:57:34.724: E/AndroidRuntime(7612):
at android.app.SharedPreferencesImpl.getInt(SharedPreferencesImpl.java:240)
12-12 19:57:34.724: E/AndroidRuntime(7612):
at sweetbix.android.shredbox.JumpSettings.loadPrefs(JumpSettings.java:228)
任何帮助将不胜感激:)
全局变量
SeekBar switchSlider;
RadioGroup directionRadioGroup;
RadioButton fsRadioBtn;
RadioButton bsRadioBtn;
RadioButton fsBsRadioBtn;
RadioButton dirOffRadioBtn;
SeekBar corkSlider;
SeekBar spinSlider;
SeekBar grabSlider;
NumberPicker minSpinNP;
NumberPicker maxSpinNP;
Button saveBtn;
boolean safe;
int maxSpinPos;
int minSpinPos;
private int switchProbability;
private int corkProbability;
private int spinProbability;
private int grabProbability;
private int spin = 0;
private int minSpin;
private int maxSpin;
private String[] spinValues = new String[8];
添加到spinValues的字符串
for (int i = 0; i < spinValues.length; i++) {
String rotation = Integer.toString(spin += 180);
spinValues[i] = rotation;
}
使用的保存方法
private void savePrefs(String key, int value){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Editor editor = prefs.edit();
editor.putInt(key, value);
editor.commit();
}
on numberlicker的onchangelistener,somecode已注释,但显示了我的尝试。
maxSpinNP.setOnValueChangedListener(new OnValueChangeListener(){
@Override
public void onValueChange(NumberPicker picker, int oldVal,
int newVal) {
// TODO Auto-generated method stub
//maxSpin = Integer.parseInt(spinValues[maxSpinNP.getValue()]);
//maxSpin = spinValues[maxSpinNP.getValue()];
maxSpinPos = Arrays.asList(spinValues).indexOf(maxSpin);
Toast.makeText(JumpSettings.this,"maxspinpos:"+ maxSpinPos,
Toast.LENGTH_SHORT).show();
}
});
调用save方法
savePrefs("MIN_SPIN_JUMPS", minSpinPos);
savePrefs("MAX_SPIN_JUMPS", maxSpinPos);
最后调用loadprefs方法,更多代码注释
private void loadPrefs(){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
switchProbability = prefs.getInt("SWITCH_JUMPS", 35);
switchSlider.setProgress(switchProbability);
corkProbability = prefs.getInt("CORK_JUMPS", 20);
corkSlider.setProgress(corkProbability);
spinProbability = prefs.getInt("SPIN_JUMPS", 80);
spinSlider.setProgress(spinProbability);
grabProbability = prefs.getInt("GRAB_JUMPS", 80);
grabSlider.setProgress(grabProbability);
minSpinPos = prefs.getInt("MIN_SPIN_JUMPS", 0);
maxSpinPos = prefs.getInt("MAX_SPIN_JUMPS", 7);
//minSpin = prefs.getString("MIN_SPIN_JUMPS", "180");
//minSpinNP.setValue(Integer.parseInt(spinValues[minSpinPos]));
//maxSpin = prefs.getString("MAX_SPIN_JUMPS", 1080);
//maxSpinNP.setValue(Integer.parseInt(spinValues[maxSpinPos]));
Log.e("min", "" + minSpinPos);
Log.e("max", "" + maxSpinPos);
//Log.e("prob", switchProbability + "");
}
这是第228行
minSpinPos = prefs.getInt("MIN_SPIN_JUMPS", 0);
答案 0 :(得分:0)
解决了它。我在加载首选项方法
中使用了它for( int i=0; i<spinValues.length ; i++ )
if( spinValues[i].equals(maxSpinPos) )
maxSpinNP.setValue(i);
numberpicker获取一个int值,我认为当我试图获取数组索引时我得到一个int值,但我实际上得到了索引并将变量设置为该索引的值
yay:)