簡體   English   中英

Android列表添加不添加哈希圖

[英]android list add is not adding hashmap

我正在使用listview在我的android應用中添加列表。 當我在哈希圖中使用for循環時,它顯示列表,但所有數據都在最終列表中,例如以下代碼運行正常。

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

它工作正常,但是當我想使用for循環添加多個項目時。 代碼如下所示。 產品是具有項目的數組列表。

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

我想要的輸出

id1名稱1 qty1類型1數量1 amt1
id2名稱2數量2類型2數量2 amt2

但是獲得輸出是

id2名稱2數量2類型2數量2 amt2
id2名稱2數量2類型2數量2 amt2

這是因為您在每次迭代中都更新了HashMap ,而是像下面這樣創建新實例

 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.

        }

在添加新值之前初始化HashMap檢查以下內容:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM