sisko
sisko

Reputation: 1

Persisting spinner selected value

In the following code I am setting up a spinner, listening and detecting selections from the spinner.

The problem is I am trying to save the previously selected value so it is persistent between activity reloads, but when I reload the activity, the previously selected value is not set as the spinner value. The code is as follows :

final Spinner spinner = (Spinner)findViewById(R.id.Spinner_gender);

  ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this, spinnerID, android.R.layout.simple_spinner_item);
  adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  spinner.setAdapter(adapter);

  spinner.setOnItemSelectedListener(
    new AdapterView.OnItemSelectedListener() {
     public void onItemSelected(AdapterView<?> parent, View itemSelected, int selectedItemPosition, long selected){
      Editor editor = mGameSettings.edit();
      editor.putLong(GAME_PREFERENCES_GENDER, selectedItemPosition);
      editor.commit();
     }

  @Override
  public void onNothingSelected(AdapterView<?> arg0) {
   // TODO Auto-generated method stub

  }

 }
  );

  //if( mGameSettings.contains(GAME_PREFERENCES_GENDER) ){
   Toast.makeText(QuizSettingsActivity.this, "Detected(again): " + spinner.getSelectedItemPosition(), Toast.LENGTH_SHORT).show();
   spinner.setSelection( spinner.getSelectedItemPosition() );
  //}

I am using spinner.getSelectedItemPosition() to get the spinner selected index. Can anyone please tell me what I am doing wrong?

Upvotes: 0

Views: 2883

Answers (2)

Michael Dilmore
Michael Dilmore

Reputation: 1

I found a solution to persisting the state of spinners in Android apps in simple situations.

In my app my spinner had three values (Normal, Important, and Urgent). Initially, these values were stored in a SQLite database as strings.

My solution involved storing the spinner values instead as integers in the SQLite database and then using the following code to set the spinner value when the activity was reloaded:

int temp = (int) memo.getInt(memo.getColumnIndexOrThrow(MemoDbAdapter.KEY_MODE));
mModeText.setSelection(temp);

This allowed me pass the data from the SQLite database directly without having to work out the position of desired selection from strings stored in the database. This worked for my simple Memo application but obviously won't work if you need to store the spinner output as strings in your database.

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006974

You are never reading from your SharedPreferences. You are writing to them (at least, I assume that is what mGameSettings is), but not reading from them.

spinner.setSelection( spinner.getSelectedItemPosition() );

This is a nonsense statement. You are setting the spinner's selection to its current selection. If you want to set the spinner's selection to the value from your SharedPreferences, you need to read the value from the SharedPreferences.

Upvotes: 2

Related Questions