質問

Okay so put simply i am trying to save and then load data to and from a NumberPicker. The NumberPicker values are set initially with a string array as specific values are required to be chosen from in the NumberPicker setDisplayedValues() method is used.

I dont have any trouble saving its just when it comes to loading. because im guessing i need to give the index of the position in which that value is found in the array. This is what i have been trying but to no avail

minSpinNP.setValue(Integer.parseInt(spinValues[minSpinPos]));
maxSpinNP.setValue(Integer.parseInt(spinValues[maxSpinPos]));

Ibelieve numberPicker must take an int or an array of strings. I am also getting this error when loading the 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)

any help would be appreciated :)

global variables

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];

strings added to spinValues

for (int i = 0; i < spinValues.length; i++) {
        String rotation = Integer.toString(spin += 180);
        spinValues[i] = rotation;
    }

the save method used,

private void savePrefs(String key, int value){
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    Editor editor = prefs.edit();
    editor.putInt(key, value);
    editor.commit();
}

onchangelistener for the numberpicker, somecode commented but shows what i have tried.

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();


        }

    });

calling the save method

savePrefs("MIN_SPIN_JUMPS", minSpinPos);
savePrefs("MAX_SPIN_JUMPS", maxSpinPos);

finally calling the loadprefs method, more code commented

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 + "");
}

this is line 228

minSpinPos = prefs.getInt("MIN_SPIN_JUMPS", 0);
役に立ちましたか?

解決

solved it. i used this in the load preferences method

for( int i=0; i<spinValues.length ; i++ )
        if( spinValues[i].equals(maxSpinPos) )
             maxSpinNP.setValue(i);

numberpicker takes an int value and i thought i was getting an int value when i was trying to get the array index but i was actually getting the index and setting the variable to the value of that index

yay :)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top