Jay Lefler
Jay Lefler

Reputation: 315

How do I set spinner value to string

I have a spinner set up like this:

        ArrayAdapter<String> states = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, getResources().getStringArray(R.array.stateabbrev));
        states.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        state.setAdapter(states);

As you can see, the source is an array.xml file.

I want to know how to populate it if I know the array value. For instance, I am retrieving information from my database and the user is from "KY" so I have a string "KY" and I want the spinner selection to be on "KY"

Upvotes: 0

Views: 4227

Answers (1)

Murat Aras
Murat Aras

Reputation: 401

at first we should get position of "KY"

int position = states.getPosition("KY");

after that, select in spinner with position

state.setSelection(position);

Upvotes: 1

Related Questions