Setting selected item of Spinner by value rather than by position

0 votes

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);
    getSpinnerField().setSelection(position);
}
Aug 10, 2018 in Java by misc.edu04
• 1,380 points
3,201 views

1 answer to this question.

0 votes

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);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
newSpinner.setAdapter(adapter);
if (compareValue != null) {
    int spinnerPosition = adapter.getPosition(compareValue);
    newSpinner.setSelection(spinnerPosition);
}
answered Aug 10, 2018 by 93.lynn
• 1,560 points

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 in Java by Priyaj
• 56,520 points
28 views
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,540 points
54 views
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  yourList.stream().forEach(a -> ...READ MORE

answered Aug 13, 2018 in Java by samarth295
• 2,190 points
77 views
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,540 points
53 views
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 parth295
• 4,620 points
648 views
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,320 points
383 views
+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,380 points

edited Nov 6, 2018 by Omkar 8,153 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,090 points
1,989 views
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,090 points
5,179 views
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,090 points
503 views