get arrayList value into ArrayAdapter for a listView in android studio


I am trying to put one piece of value in an ArrayList to my listView, below are the details.

Here is the sample of the ArrayList info:

private static final ArrayList<User> users = new ArrayList<User>(){{
        add(new User("Nevin Hobden",38,"Male","Friend","New Mexico","NM"));
        add(new User("Stillman Macken",32,"Male","Friend","Arizona","AZ"));
        add(new User("Stevy Ranscomb",36,"Male","Friend","Arizona","AZ"));
        add(new User("Lynelle Garstang",22,"Female","Family","California","NE"));

I want to grab the state data out from users ArrayList, eg. I only wish to get "New Mexico", "Arizona", "Arizona", and "California" data out to show it on my listView

if that is possible I also want to remove the duplicate and sort in ascending order

   New Mexico

Below is the code I had

   ListView stateListView;
   ArrayList<DataServices.User> stateList = DataServices.getAllUsers();
   ArrayAdapter<DataServices> arrayListAdapter;

   stateListView = view.findViewById(;
   arrayListAdapter = new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,, stateList);

I know I should not use stateList in the arrayAdapter however I have no idea how should I change for that part, any help is appreciated. Thanks!


If you want to get a list of the states, you can just loop over the original list and put the state into an ArrayList<String>, like this

ArrayList<String> states = new ArrayList<>();
for(DataServices.User user : users) {

If you only want unique states, you could just use a HashSet there instead. Sets eliminate duplicates.

HashSet<String> states = new HashSet<>();
for(User user : users) {

// then convert it back to a list
ArrayList<String> unique_states = new ArrayList<>(states);

And if you want it to be sorted, you can use Collections.sort to sort the list


If your version of Java supports it, you can also do this in fewer lines with streams, but it accomplishes the same thing

List<String> statesList =
                               .map(u -> u.state)

Set<String> statesSet =
                             .map(u -> u.state)

// Or in one-line
List<String> unique_states =
                .map(u -> u.state)

Answered By – Tyler V

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published