4

Im using the "SharedPreferences" in my app to retain the ability to save/retrieve string values from multiple edittext boxes, and thats working just fine. I also have a Spinner in my activity with a string-array for it's usable values. But I am unclear on how to write the spinners selection to the SharedPreferences, then read the SharedPreferences later to retireve and set it's value.

Here is the configuration I have for the edittext's:

-Button to activate save values to SharedPreferences-

public void buttonSaveSendClick(View view) {

    SharedPreferences.Editor editor = getPreferences(0).edit();

    EditText editTextCallId = (EditText) findViewById(R.id.editTextCallId);
    editor.putString("editTextCallIdtext", editTextCallId.getText().toString());
    editor.putInt("selection-startCallId", editTextCallId.getSelectionStart());
    editor.putInt("selection-endCallId", editTextCallId.getSelectionEnd());
    editor.commit();
}

-Button to activate restore last saved values from SharedPreferences-

public void buttonRestoreLastClick(View view) {

    SharedPreferences prefs = getPreferences(0); 

    EditText editTextCallId = (EditText) findViewById(R.id.editTextCallId);
    String editTextCallIdtextrestored = prefs.getString("editTextCallIdtext", null);
    editTextCallId.setText(editTextCallIdtextrestored, EditText.BufferType.EDITABLE);
    int selectionStartCallId = prefs.getInt("selection-startCallId", -1);
    int selectionEndCallId = prefs.getInt("selection-endCallId", -1);
    editTextCallId.setSelection(selectionStartCallId, selectionEndCallId);
}

Any suggestions on how to construct the collection of the spinner's selected value in the first button (save)? Then how to return that saved value to the spinners view on press of the "restore" button?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Devin
  • 257
  • 2
  • 3
  • 10

1 Answers1

7

You have to call editor.apply(); once after all of your editor.put(); statements. Otherwise all of the changes that you've made to the preferences will get discarded. Assuming that the items in your array won't be changing position at all then you can just store the selected position as an int in your preferences.

to Save:

int selectedPosition = yourSpinner.getSelectedItemPosition();
editor.putInt("spinnerSelection", selectedPosition);
editor.apply();

to Load:

yourSpinner.setSelection(prefs.getInt("spinnerSelection",0));

If the items in your array are going to change then you'll have to store the actual string, instead of the position. Something like this would work:

String selectedString = yourArray[yourSpinner.getSelectedItemPosition()];
editor.putString("spinnerSelection", selectedString);
editor.apply();

and

find the position of the string by looping through your array and checking array[i] against the value stored in prefs. Then call yourSpinner.setSelected(position). If you use an ArrayList instead this part could be done without the loop by calling

ArrayList.indexOf(prefs.getString("spinnerSelection", ""));

Be aware that only ArrayList does have an indexOf(); method. On a plain Array you can not use the indexOf(); method, you'll have to manually search your Array to find the right value.

nandur
  • 134
  • 10
FoamyGuy
  • 46,603
  • 18
  • 125
  • 156