GoCrazy GoCrazy - 1 year ago 81
Android Question

How to set Spinner Default by its Value instead of Postion?

I have 1-50 records in the database. I am fetching those data using cursor and set those values to Spinner using Simple Cursor Adapter. Now what i need is i want to set one value say 39th value as default. But not by its position i want to set by its value.

I know how to set the spinner default by its position


will set the spinner to that value.

But i didn't have any idea about setting the spinner default by its value(text) in the database.
I know the values in the database. For eg "books" is one of the value in the spinner. I need to set the spinner default as books.

Is there any possible way to do this?

Thanks for you help guys...


Finally, i solved the problem by using following way, in which the position of the spinner can be get by its string

private int getPostiton(String locationid,Cursor cursor)
    int i;
    for(i=0;i< cursor.getCount()-1;i++)

        String locationVal = cursor.getString(cursor.getColumnIndex(RoadMoveDataBase.LT_LOCATION));  
            position = i+1;  
            position = 0;

Calling the method

    Spinner location2 = (Spinner)findViewById(R.id.spinner1);
    int location2id = getPostiton(cursor.getString(3),cursor);

I hope it will help for some one..