How to maintain spinner state in android -


i'm doing android app can data web service & load spinner. need maintain selected data state of spinner while go screen & come back. example, if i'm getting data web service 1.apple 2.orange 3.grapes & loading spinner, select orange. when go other screen & come back, selected spinner data should orange. again loads data server spinner. can me resolve this?

my code:

@override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);      //...      if (constants.spinner != null ) {         spinner.setselection( constants.spinner);     } else {         //webcall here getting data     }      //...      spinner.setonitemselectedlistener(new onitemselectedlistener() {         public void onitemselected(adapterview<?> parent,             view view, int arg2, long arg3) {                 constants.spinner = spinner.getselecteditemposition(); 

in constant class:

public static integer spinner=""; 

you can use

  spinner.getselecteditemposition();  

that return int can save

  prefedit.putint();  

then when want re-load has been saved call

  spinner.setselection(prefs.getint("key", default)); 

or try this

to save:

  int selectedposition = yourspinner.getselecteditemposition()   editor.putint("spinnerselection", selectedposition);   editor.commit(); 

to load:

yourspinner.setselection(prefs.getint("spinnerselection",0)); 

if array used should changed this

 string selectedstring = yourarray[yourspinner.getselecteditemposition()];    editor.putstring("spinnerselection", selectedstring);    editor.commit(); 

checking array[i] against value stored in prefs.if use arraylist instead part done without loop calling

 arraylist.indexof(prefs.getstring("spinnerselection", ""); 

when commit show above array item gone. show no 1 array.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -