How to set selected item of Spinner by value, not by position?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In Android development, a Spinner widget serves as a drop-down list from which the user can select a single value. While it is common to select an item in a Spinner based on its position in the list, sometimes it is necessary to set the Spinner’s value based on the item value itself rather than its position. This approach can be particularly useful in dynamic lists where the positions might change due to operations like sorting or filtering.
Understanding Spinner and ArrayAdapter
A Spinner in Android is typically backed by an ArrayAdapter or a similar adapter which can arrange array-based data for the Spinner. The ArrayAdapter is initialized with a list of items, which are then presented in the Spinner view. Each item in an ArrayAdapter is typically an instance of a class, or simply a String.
Here is an example of initializing a Spinner with an ArrayAdapter using a list of strings:
Setting Selected Item by Value
To set the selected item by value, you first need to find the index of this value in the array or list backing the ArrayAdapter. Once the index is found, you can then set the Spinner’s selection to this index.
Below is a method demonstrating how this can be done:
In this method, getPosition is a handy method provided by ArrayAdapter that returns the index of the specified item (value). Once you get this index, you can use it to set the Spinner's selected item using setSelection.
Handling Custom Objects
The example above assumes that the Spinner is using an ArrayAdapter with a list of strings. If you are working with a list of custom objects, the process requires a modification. You first need to override the toString() method in your custom object class to return a string value that represents the object appropriately.
For instance, consider a custom class Person with properties name and id. Here is how you might override toString():
Then, you need a method similar to the previous example to set the Spinner selection:
This method uses a loop to compare the personName with the name property of each Person object in the adapter.
Summarizing Key Points
Here is a summary of the key points discussed:
| Action | Method | Usage Context |
| Get Position from value | adapter.getPosition(value) | Simple string lists |
| Set Spinner selection | spinner.setSelection(position) | After finding position |
| Handle custom objects | Customize toString() in object's class | Custom object lists |
| Compare and set selection | Loop and use equals() on toString() results | Custom object lists |
Conclusion
Using value rather than position to set the selected item in a Spinner can be more intuitive, especially in cases where the list of data may be dynamic or contain complex objects. By leveraging the ArrayAdapter methods and customizing object representation through toString(), developers can efficiently manipulate the user interface elements in Android to reflect the data correctly and intuitively.
This approach not only allows more flexibility in handling user interactions but also helps maintain consistency and accuracy in the application’s data flow, thereby enhancing user experience.

