user1410081
user1410081

Reputation:

how to save spinner selecteditem to sharedpreferences?

I already know how to save edittext contents to sharedpreferences but in spinners and radiogroups I still don't have a clue. Can you please give me snippets of codes how to do it? Thanks

Upvotes: 1

Views: 2596

Answers (2)

Ali
Ali

Reputation: 9994

This is the way that you can save the selected item of a spinner in sharedPreferences:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
     public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
       Object obj = parent.getItemAtPosition(pos);
       SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
       Editor prefsEditor = prefs.edit();
       prefsEditor.putString("object", obj.toString());
       prefsEditor.commit();            
    }
    public void onNothingSelected(AdapterView<?> parent) { }
});

Upvotes: 0

Stefan
Stefan

Reputation: 4705

For the data store is is not relevant which UI elements is used to display or modify the value. Here is a description how to store or retrieve various data types: http://developer.android.com/reference/android/content/SharedPreferences.html

So a spinner selection is simply an integer (or string if you like) and the choice for a radio group is simply whatever identifier (as string) you choose to represent that choice. If the choices come from an array resource you may use the values from the array or the index into the array. Store/retrieve them in/from the shared preferences like you used to store and retrieve the text from the EditText.

Upvotes: 1

Related Questions