vendredi 20 janvier 2017

How to save and retrieve value / selection of a Spinner and Check Box in Android shared preferences?

I am trying to save and retrieve information the user enters in a edit box and the selections made on check box and spinner. I have managed to get the edit box working but after several hours I still can't work out how to do it with the Spinner and Checkbox.

This is for the Edit Box and it works fine to save the information.

editor.putString("saveName", name.getText().toString());

But these don't... for the Spinner and Checkbox

editor.putInt("saveGender", (Integer) Gender.getSelectedItem());
editor.putBoolean("saveChoice1", choice1.isChecked());

This is for the Edit Box and it works fine to read the information.

editor.putString("saveName", name.getText().toString());

But these don't... for the Spinner and Checkbox

Gender.setSelection(sharedPref.getInt("savedGender", 0));
choice1.setSelected(sharedPref.getBoolean("saveChoice1", false));

I get the error below, which I assume refers to the Spinner and for the case of check box no errors are displayed but it still doesn't work.

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

I have spent a lot of time doing this and would really appreciate any help!




Aucun commentaire:

Enregistrer un commentaire