Setting selected item of Spinner by value rather than by position

+1 vote

I want to preselect the value that is stored in the database for a Spinner but I can't find the indexOf method of the Adapter. Can someone help me out of this?

void setSpinner(String value)
    int position = getSpinnerField().getAdapter().indexOf(value);
Aug 10, 2018 in Java by misc.edu04
• 1,430 points

1 answer to this question.

+1 vote

Suppose your Spinner is named newSpinner, and it contains as one of its choices: "xyz".

To find and compare the position of "xyz" in the Spinner, you can use the following:

String compareValue = "xyz";
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.select_state, android.R.layout.simple_spinner_item);
if (compareValue != null) {
    int spinnerPosition = adapter.getPosition(compareValue);
answered Aug 10, 2018 by 93.lynn
• 1,610 points
my problem was how to get the position of an item in a spinner. this snippet solved it.
Glad I could help. Please do upvote my answer if it has helped. I need more points and reputation.

Related Questions In Java

0 votes
1 answer

Rotate elements of array by n position

Here is what I came up with,  import ...READ MORE

answered Mar 3, 2019 in Java by Priyaj
• 57,530 points
0 votes
1 answer

Sort a Map<Key, Value> by values

This might help solve your problem...!!!! public class ...READ MORE

answered Apr 18, 2018 in Java by Rishabh
• 3,580 points
0 votes
2 answers

How to sort an ArrayList of custom object by property in Java?

You can Sort using java 8 yourList.sort(Comparator.comparing(Classname::getName)); or -> ...READ MORE

answered Aug 13, 2018 in Java by samarth295
• 2,190 points
0 votes
1 answer

Is Java “pass-by-reference” or “pass-by-value”?

Think of reference parameters as being aliases ...READ MORE

answered Jun 8, 2018 in Java by Rishabh
• 3,580 points
0 votes
1 answer

No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK

In your Eclipse IDE, go into Window > Preferences > Java > Installed JREs > ...READ MORE

answered Jun 18, 2018 in Java by Parth
• 4,640 points
0 votes
1 answer

A Java collection of value pairs? (tuples?)

The Pair class is one of those ...READ MORE

answered Jul 2, 2018 in Java by sharth
• 3,370 points
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,400 points

edited Nov 6, 2018 by Omkar 16,669 views
0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,510 points
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,510 points
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,510 points