Thamaraiselvam
Thamaraiselvam

Reputation: 7080

How to parse List<Map> in android?

Im trying to store data like associative array in JAVA. so i took list with maps

List<Map> processNeedImages = null; 
int flag =0;
if(c.moveToFirst()){
    do{
         Map<String, String> map = null;
         map.put("status", c.getString(c.getColumnIndex("system_url")));
         processNeedImages.add(flag++, map);
      }while(c.moveToNext());

I could not parse this data

List<Map> allImages = getData();
for (Map map:allImages){
    Log.d("listImage", String.valueOf(map.get("status")));
}

Even loop is iterating even once.

Upvotes: 1

Views: 381

Answers (2)

ELITE
ELITE

Reputation: 5940

Initialize all variable in your code like below

List<Map> processNeedImages = new ArrayList<>(); 
int flag =0;
if(c.moveToFirst()){
    do{
         Map<String, String> map = new HashMap<>();
         map.put("status", c.getString(c.getColumnIndex("system_url")));
         processNeedImages.add(flag++, map);
      }while(c.moveToNext());
}

this will work fine.

You should read this answer too what is NullPointerException

Upvotes: 1

Ankit Aggarwal
Ankit Aggarwal

Reputation: 5375

Map<String, String> map = null

should be replaced with

Map<String, String> map = new HashMap<>(); 

You are not creating a new map object. Rather you are adding the same map object everytime. although you are adding new values to the map for each cursor position but the object is only one. Hence your loop is going only one time.

Upvotes: 1

Related Questions