java - Taking user selection in a spinner, storing it in sharedPreferences, and using it in another activity -


i need user choose restaurant, got pretty large list different selections. need save choice, preferably in sharedpreferences. , use in activity display data excel document.

currently code looks this:

in oncreate:

resturantspinner = (spinner) findviewbyid(r.id.resturantspinner);      // create arrayadapter using string array , default spinner layout     arrayadapter<charsequence> adapter = arrayadapter.createfromresource(this,             r.array.resturant_arrays, android.r.layout.simple_spinner_item);     // specify layout use when list of choices appears     adapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item);    // apply adapter spinner     resturantspinner.setadapter(adapter); 

onitemselected:

public void onitemselected(view view) {       int userchoice = resturantspinner.getselecteditemposition();       sharedpreferences sharedpref = getsharedpreferences("resturantfile",0);       sharedpreferences.editor prefeditor = sharedpref.edit();       prefeditor.putint("userchoicespinner", userchoice);       prefeditor.commit(); } 

and retrieving data, in activity:

resturanttextview = (textview) findviewbyid(r.id.resturantchoice);      intent intent = getintent();      sharedpreferences sharedpref = getsharedpreferences("resturantfile",mode_private);     int resturantchoice = sharedpref.getint("userchoicespinner",-1);      resturanttextview.settext("" + resturantchoice); 

i use textview see saves like, , shows -1

edit: might add this, userchoice value 0.

try this:

add sharedpreferences sharedpref = getsharedpreferences("resturantfile",activity.mode_private) instead of
sharedpreferences sharedpref = getsharedpreferences("resturantfile",0).

for example: way save

sharedpreferences sp = getsharedpreferences("your_prefs", activity.mode_private); sharedpreferences.editor editor = sp.edit(); editor.putint("your_int_key", yourintvalue); editor.commit(); 

the way retrieve data in activity:

sharedpreferences sp = getsharedpreferences("your_prefs", activity.mode_private); int myintvalue = sp.getint("your_int_key", -1); 

if not working try one: change default value -1 0,

int myintvalue = sp.getint("your_int_key", 0); 

for more information use question.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -