0

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

1
  • It is updating not creating new. use HashMap<String,String> temp=new HashMap<String, String>(); inside loop Commented Feb 15, 2018 at 4:55

2 Answers 2

4

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.

        }
Sign up to request clarification or add additional context in comments.

Comments

1

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);
}

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.