繁体   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