简体   繁体   中英

android list add is not adding hashmap

I am using using listview for adding list in my android app. when i am using for loop in hashmap it showing the list but all the datas are in final list for example the below code is working as fine.

products = new ArrayList<Cash>(products);   
HashMap<String,String> temp=new HashMap<String, String>();
list=new ArrayList<HashMap<String,String>>();

temp.put("item1", "id1";
temp.put("item2", "name1");
temp.put("item3", "qty1");
temp.put("item4", "type1");
temp.put("item5", "quantity1");
temp.put("item6", "amt1");

list.add(temp);

temp1.put("item1", "id2";
temp1.put("item2", "name2");
temp1.put("item3", "qty2");
temp1.put("item4", "type2");
temp1.put("item5", "quantity2");
temp1.put("item6", "amt2");

list.add(temp1);
ListViewAdapters adapter1=new ListViewAdapters(getActivity(), list);
listView.setAdapter(adapter1);

It's working as fine but when i want to add multiple items using for loop. The code is shown below. The products is the array list having the items.

    products = new ArrayList<Cash>(products);
    HashMap<String,String> temp=new HashMap<String, String>();
    list=new ArrayList<HashMap<String,String>>();

     for (int i=0; i<products.size();i++){
            Log.d("LOG","Product size:"+products.size());
            Log.d("LOG","Product name:"+products.get(i).getName());
            Log.d("LOG","Product amt:"+products.get(i).getAmount());
            temp.put("item1", String.valueOf(i));
            temp.put("item2", products.get(i).getName());
            temp.put("item3", products.get(i).getQuantity());
            temp.put("item4", products.get(i).getType());
            temp.put("item5", products.get(i).getQuantity());
            temp.put("item6", products.get(i).getAmount());

            list.add(i, temp);
            //list.

        }

    ListViewAdapters adapter1=new ListViewAdapters(getActivity(), list);
    listView.setAdapter(adapter1);

The output i want

id1 name1 qty1 type1 quantity1 amt1
id2 name2 qty2 type2 quantity2 amt2

But getting output is

id2 name2 qty2 type2 quantity2 amt2
id2 name2 qty2 type2 quantity2 amt2

It's because you are updating HashMap in every iteration, instead create new instance as in

 for (int i=0; i<products.size();i++){
            Log.d("LOG","Product size:"+products.size());
            Log.d("LOG","Product name:"+products.get(i).getName());
            Log.d("LOG","Product amt:"+products.get(i).getAmount());

            temp = new HashMap<String,String>(); //<-- add this line

            temp.put("item1", String.valueOf(i));
            temp.put("item2", products.get(i).getName());
            temp.put("item3", products.get(i).getQuantity());
            temp.put("item4", products.get(i).getType());
            temp.put("item5", products.get(i).getQuantity());
            temp.put("item6", products.get(i).getAmount());

            list.add(i, temp);
            //list.

        }

Initialize HashMap before you adding new values.Check below:

temp=new HashMap();

for (int i=0; i<products.size();i++){
    temp = new HashMap<String, String>();
    Log.d("LOG","Product size:"+products.size());
    Log.d("LOG","Product name:"+products.get(i).getName());
    Log.d("LOG","Product amt:"+products.get(i).getAmount());
    temp.put("item1", String.valueOf(i));
    temp.put("item2", products.get(i).getName());
    temp.put("item3", products.get(i).getQuantity());
    temp.put("item4", products.get(i).getType());
    temp.put("item5", products.get(i).getQuantity());
    temp.put("item6", products.get(i).getAmount());

    list.add(i, temp);
}

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