简体   繁体   中英

Cannot retrieve values from hashmap embeded within another hashmap

I have a data within arraylist in following format:

ArrayList<HashMap<String, HashMap<String, String>>>  data;

I am retrieving data from JSON and binding them in the arraylist.Sample values of the arraylist are:

11-25 11:55:19.968: I/System.out(16451): Arraylist-->[{0={personid=64, personname=Biki Poddar, personage=19, personimage=1448347571_u.png, dogimage=1448347571_dog.png, dogname=sog, dogage=12}}, {1={personid=48, personname=Aron, personage=23, personimage=1447653750_u.png, dogimage=1447757386_dog.jpg, dogname=doggy, dogage=12}}, {2={personid=46, personname=Aron, personage=23, personimage=1447653797_u.png, dogimage=1447477714_dog.jpg, dogname=doggy, dogage=12}}, {3={personid=24, personname=name, personage=22, personimage=1447321030_u.jpg, dogimage=1447755229_dog.jpg, dogname=tommy123, dogage=0}}, {4={personid=45, personname=Aron, personage=23, personimage=1447653811_u.png, dogimage=1447318926_dog.jpg, dogname=doggy, dogage=12}}, {5={personid=44, personname=Aron, personage=23, personimage=1447653821_u.png, dogimage=1447318926_dog.jpg, dogname=doggy, dogage=12}}, {6={personid=43, personname=Aron, personage=23, personimage=1447653841_u.png, dogimage=1447318926_dog.jpg, dogname=doggy, dogage=12}}, {7={personid=40, personname=Aron, personage=23, personimage=1447653881_u.png, dogimage=1447318926_dog.jpg, dogname=doggy, dogage=12}}]

Now I try to retrieve values from the arraylist within an adapter as:

HashMap<String, HashMap<String, String>> resultp = new HashMap<String, HashMap<String, String>>();
resultp = data.get(position);

where data is:

ArrayList<HashMap<String, HashMap<String, String>>>  data;

The result of resultp :

11-25 11:55:20.128: I/System.out(16451): resultp--->{0={personid=64, personname=Biki Poddar, personage=19, personimage=1448347571_u.png, dogimage=1448347571_dog.png, dogname=sog, dogage=12}}

Now I want to fetch the value of the hashmap within resultp and I cannot do that.I have tried several ways, using entryset as well as creating a new hashmap etc,but I ended up with nothing.Please help.

This for-each loop gets every HashMap entry in your resultp

for (String key : resultp.keySet()) { /* This line will get you every key (0, 1,2 3...)*/
   HashMap<String, String> entry = resultp.get(key); /*This line will get you every entry for each key*/

   for (String secondKey : entry.keySet()) { /*This line will get you every key for your entry 0, 1, 2 etc */
        String content = entry.get(secondKey);/* Do what you want with the result */
   }
}

Your initial data structure is an array list and hence you retrieve the members through index. ( data.get(index) )

As the list contains a map inside it, the result of above operation is a map.

Now consider resultp as just a map with key of type String and values as objects(another map in this case). Using resultp.get(key) will return an object of type HashMap<String, String>

 HashMap<String, String> innerMap = resultp.get(key);

Now this is the innermost map where usual map operations will be applicable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM