Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (3.9k points)

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

I was having in mind something like this, but the Adapter has no indexOf method, so I am stuck.

void setSpinner(String value)

{

    int pos = getSpinnerField().getAdapter().indexOf(value);

    getSpinnerField().setSelection(pos);

}

1 Answer

0 votes
by (46k points)

Assume your Spinner is named mSpinner, and it includes as one of its options: "any 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);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

mSpinner.setAdapter(adapter);

if (compareValue != null) {

    int spinnerPosition = adapter.getPosition(compareValue);

    mSpinner.setSelection(spinnerPosition);

}

Related questions

0 votes
1 answer
asked Feb 25, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...