David
David

Reputation: 383

Get Spinner position and get it selected

I need save in prefs the value or position of my Spinner and when the users access to settings activity show it as selected. But I'm having troubles.My prob is in the for to get the item position and use it as selected on the 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 WITH 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>

Upvotes: 0

Views: 1369

Answers (3)

n8yn8
n8yn8

Reputation: 4513

I think a better method to get the item that is chosen in the spinner is in the onItemSelected method, use the position variable to get the item at that position.

item = parent.getItemAtPostion(position).toString();

Then, when a user navigates back to the view with this spinner, you can set the position of the spinner by first finding where the item is located (since you are using an ArrayAdapter<CharSequence>) in the adapter and then setting the position of the spinner from where the adapter said it would be located.

String spinner=PreferenceManager
                 .getDefaultSharedPreferences(getBaseContext())
                 .getString("spinner","");
int position = adapter.getPosition(item);
spinner.setSelection(position);

Upvotes: 1

Pararth
Pararth

Reputation: 8134

No useful relation between for loop and if condition :

for(int i=0;i<9;i++)

if(spinner.equals(parent.getSelectedItem().toString())){
spinner.setSelection(parent.getItemAtPosition(position));

[ should use the "i" from the loop like parent.getItemAtPosition(i).toString() ]

Upvotes: 0

reidisaki
reidisaki

Reputation: 1524

this might help

if(spinner.equals(parent.getSelectedItem().toString())){

change that to

if(item.equals(parent.getSelectedItem().toString())){

If i understand your question correctrly..

Upvotes: 1

Related Questions