In the Android 2.2 SDK there's a sample app (new for 2.2) called
"Spinner" that demonstrates how to do this using SharedPreferences. It
will save the state of the spinner across pause() and resume(), and
also across app shutdown and restart.

Even better, it comes with a test package project called SpinnerTest
that demonstrates the unit test for this.

And best of all, the Activity Testing tutorial uses this app and its
test package to demonstrate unit testing in Android.

Enjoy!

The Elk

On Nov 7, 3:09 pm, sisko <adeod...@gmail.com> wrote:
> Hi guys,
>
> I have a spinner successfully working but I am trying to use the
> spinner.setSelection method to "remember" the previous spinner value
> when the activity is reloaded.
>
> Outside of setOnItemSelectedListener, is there a function of the
> spinner that can give me the selected index?
>
> The following code is my function code to both setup the spinner and
> then my attempt to set the spinner with the index previously selected:
>
> private void setSpinner(int spinnerID){
>                 final Spinner spinner   =
> (Spinner)findViewById(R.id.Spinner_gender);
>                 int spinner_value;
>
>                 spinner.setOnItemSelectedListener(
>                                 new AdapterView.OnItemSelectedListener() {
>                                         public void 
> onItemSelected(AdapterView<?> parent, View
> itemSelected, int selectedItemPosition, long selected){
>                                                 spinner_value   =       
> selectedItemPosition;
>                                                 Editor editor   =       
> mGameSettings.edit();
>                                                 
> editor.putLong(GAME_PREFERENCES_GENDER,
> selectedItemPosition);
>                                                 editor.commit();
>                                                 
> Toast.makeText(QuizSettingsActivity.this, "selected:
> "+GAME_PREFERENCES_GENDER+"("+ spinner_value +")",
> Toast.LENGTH_SHORT).show();
>                                         }
>
>                                                 @Override
>                                                 public void 
> onNothingSelected(AdapterView<?> arg0) {
>                                                         // TODO 
> Auto-generated method stub
>
>                                                 }
>
>                                         }
>                 );
>
>                 ArrayAdapter<?> adapter   =       
> ArrayAdapter.createFromResource(this,
> spinnerID, android.R.layout.simple_spinner_item);
>
> adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_it 
> em);
>                 spinner.setAdapter(adapter);
>                 if( mGameSettings.contains(GAME_PREFERENCES_GENDER) ){
>                         spinner.setSelection(spinner_value);
>                 }
>
>         }
>
> All I am trying to do here is use spinner_value to remember the
> selected index so I can set the spinner to that index.
> However, the spinner_value is never holding the desired value at
> spinner.setSelection(spinner_value); in the last lines of code

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to