7

I have an ArrayList object like this:

ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();

How to iterate through the list? I want to display the value in a TextView which comes from the data of ArrayList object.

2 Answers 2

22

Simplest is to iterate over all the HashMaps in the ArrayList and then iterate over all the keys in the Map:

TextView view = (TextView) view.findViewById(R.id.view);

for (HashMap<String, String> map : data)
     for (Entry<String, String> entry : map.entrySet())
         view.append(entry.getKey() + " => " + entry.getValue());
Sign up to request clarification or add additional context in comments.

2 Comments

For the 2nd iteration I personally prefer to iterate of the map entries, instead of the keys. for (Entry<String, String> entry : map.entrySet()). That way you already both the key and the value, you don't need another map lookup.
@BenvanGompel: Good point, depends on your use case, but updated! :)
2

for(HashMap<String, String> map : data){ ... deal with map... }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.