I have ArrayList>. In another activity I want to access all values stored in ArrayList>.
I have tried following code:
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
for(Hashmap<String, String> map: mylist) {
for(Entry<String, String> mapEntry: map) {
String key = mapEntry.getKey();
String value = mapEntry.getValue();
}
}
but it shows an error at for(Entry<String, String> mapEntry: map)
that it only interate over Array.
An element can be retrieved from the ArrayList in Java by using the java. util. ArrayList. get() method.
To retrieve the set of keys from HashMap, use the keyset() method. However, for set of values, use the values() method.
A HashMap contains key-value pairs, there are three ways to convert a HashMap to an ArrayList: Converting the HashMap keys into an ArrayList. Converting the HashMap values into an ArrayList. Converting the HashMap key-value pairs into an ArrayList.
Your code has bit different for this line,
for(Entry<String, String> mapEntry: map.entrySet())
Try this and let me know what happen,
for (HashMap<String, String> map : mylist)
for (Entry<String, String> mapEntry : map.entrySet())
{
String key = mapEntry.getKey();
String value = mapEntry.getValue();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With