Pentium10 Pentium10 - 1 year ago 127
Java Question

How to set selected item of Spinner by value, not by position?

I have a update view, where I need to preselect the value stored in database for a Spinner.

I was having in mind something like this, but the

has no
method, so I am stuck.

void setSpinner(String value)
int pos = getSpinnerField().getAdapter().indexOf(value);

Answer Source

Suppose your Spinner is named mSpinner, and it contains as one of its choices: "some value".

To find and compare the position of "some value" in the Spinner use this:

String compareValue = "some value";
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.select_state, android.R.layout.simple_spinner_item);
if (!compareValue.equals(null)) {
    int spinnerPosition = adapter.getPosition(compareValue);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download