Android - How to get all items in a Spinner?

戏子无情 提交于 2019-12-04 03:17:49

A simple and elegant way to do this is, if you know the objects type that the spinner is storing:

public class User {

    private Integer id;

    private String name;

    /** Getters and Setters **/

    @Override
    public String toString() {
        return name;
    }
}

Given the previous class and a Spinner that contains a list of User 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;
}

This helped me! I hope it would do it for you!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!