Android - How to get all items in Spinner?

How to get all the elements in Spinner?

I had trouble finding a way to get all the elements from Spinner , but I could not find an elegant solution. The only solution seems to be to save the list of elements before adding it to Spinner

Is there any better way to do this?

+4
source share
1 answer

A simple and elegant way to do this if you know the type of objects that the counter stores:

 public class User { private Integer id; private String name; /** Getters and Setters **/ @Override public String toString() { return name; } } 

Given the last class and the Spinner containing the User list, you can do the following:

 public List<User> retrieveAllItems(Spinner theSpinner) { Adapter adapter = theSpinner.getAdapter(); int n = adapter.getCount(); List<User> users = new ArrayList<User>(n); for (int i = 0; i < n; i++) { User user = (User) adapter.getItem(i); users.add(user); } return users; } 

It helps me! Hope he does it for you!

+9
source

All Articles