Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android列表添加未添加hashmap_Java_Android_Listview_Arraylist_Hashmap - Fatal编程技术网

Java android列表添加未添加hashmap

Java android列表添加未添加hashmap,java,android,listview,arraylist,hashmap,Java,Android,Listview,Arraylist,Hashmap,我正在使用listview在我的android应用程序中添加列表。当我在hashmap中使用for循环时,它显示列表,但所有数据都在最终列表中。例如,下面的代码工作正常 products = new ArrayList<Cash>(products); HashMap<String,String> temp=new HashMap<String, String>(); list=new ArrayList<HashMap<String,Str

我正在使用listview在我的android应用程序中添加列表。当我在hashmap中使用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);
products=新阵列列表(products);
HashMap temp=新的HashMap();
列表=新的ArrayList();
临时投入(“项目1”、“id1”;
临时投入(“项目2”、“名称1”);
临时投入(“项目3”、“数量1”);
临时投入(“项目4”、“类型1”);
临时投入(“第5项”、“数量1”);
临时投入(“第6项”、“amt1”);
列表。添加(临时);
temp1.put(“item1”、“id2”;
临时1.投入(“项目2”、“名称2”);
临时1.出售(“第3项”、“第2数量项”);
temp1.put(“第4项”、“第2类”);
临时1.投入(“第5项”、“数量2”);
临时1.出售(“第6项”、“amt2”);
列表。添加(temp1);
ListViewAdapters adapter1=新的ListViewAdapters(getActivity(),list);
setAdapter(适配器1);
它工作得很好,但是当我想使用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);
products=新阵列列表(products);
HashMap temp=新的HashMap();
列表=新的ArrayList();

对于(int i=0;i在添加新值之前初始化
HashMap
。请检查以下内容:

temp=newhashmap();

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

for(int i=0;i在添加新值之前初始化
HashMap
。请检查以下内容:

temp=newhashmap();

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

for(inti=0;i这是因为您在每次迭代中都要更新
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.

        }

for(inti=0;i这是因为您在每次迭代中都要更新
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.

        }

for(int i=0;iIt正在更新而不是创建新的。使用
HashMap temp=new HashMap();
内部循环它正在更新而不是创建新的。使用
HashMap temp=new HashMap();
内部循环