java - Get Spinner position and get it selected -


i need save in prefs value or position of spinner , when users access settings activity show selected. i'm having troubles.my prob in item position , use selected on list.

spinner code

    spinner spinner = (spinner) findviewbyid(r.id.spinner1);     arrayadapter<charsequence> adapter = arrayadapter.createfromresource(             this, r.array.spinner, android.r.layout.simple_spinner_item);     adapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item);     spinner.setadapter(adapter);      spinner.setonitemselectedlistener(new onitemselectedlistener() {          public void onitemselected(adapterview<?> parent, view view,                 int position, long id) {              item = parent.getselecteditem().tostring();             log.v("spinner", item);               //save prefs spinner value              sharedpreferences prefs;              prefs = preferencemanager.getdefaultsharedpreferences(getbasecontext());              sharedpreferences.editor prefeditor = prefs.edit();              prefeditor.putstring("spinner",item);              prefeditor.commit();              //retreive spinner vale prefs              string spinner=preferencemanager                      .getdefaultsharedpreferences(getbasecontext())                      .getstring("spinner",""); //check  for(int i=0;i<9;i++) if(spinner.equals(parent.getselecteditem().tostring())){      spinner.setselection(parent.getitematposition(position));      break; }          }          public void onnothingselected(adapterview<?> parent) {             //string str = "selecciona";         }      }); 

xml

<?xml version="1.0" encoding="utf-8"?> <resources>     <string-array name="spinner">         <item>10:00</item>         <item>11:00</item>         <item>12:00</item>         <item>13:00</item>         <item>14:00</item>         <item>15:00</item>         <item>16:00</item>         <item>17:00</item>         <item>18:00</item>         <item>19:00</item>         <item>20:00</item>         <item>21:00</item>         <item>22:00</item>         <item>23:00</item>         <item>24:00</item>     </string-array> </resources> 

this might help

if(spinner.equals(parent.getselecteditem().tostring())){ 

change to

if(item.equals(parent.getselecteditem().tostring())){ 

if understand question correctrly..


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -