Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.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 Hashmap到ArrayList的For循环未包含正确的值。如何修复?_Java_Android_Arraylist_Hashmap_Simpleadapter - Fatal编程技术网

Java Hashmap到ArrayList的For循环未包含正确的值。如何修复?

Java Hashmap到ArrayList的For循环未包含正确的值。如何修复?,java,android,arraylist,hashmap,simpleadapter,Java,Android,Arraylist,Hashmap,Simpleadapter,我有下面的代码,它出人意料地不起作用 needsInfoView = (ListView) findViewById(R.id.needsInfo); needsInfoList = new ArrayList<>(); HashMap<String, String> needsInfoHashMap = new HashMap<>(); for (int i = 0; i &

我有下面的代码,它出人意料地不起作用

     needsInfoView = (ListView) findViewById(R.id.needsInfo);
            needsInfoList = new ArrayList<>();
            HashMap<String, String> needsInfoHashMap = new HashMap<>();

            for (int i = 0; i < 11; i++) {
                needsInfoHashMap.put("TA", needsTitleArray[i]);
                needsInfoHashMap.put("IA", needsInfoArray[i]);
                Log.e("NIMH",needsInfoHashMap.toString());
//Here, I get the perfect output - TA's value, then IA's value
                needsInfoList.add(needsInfoHashMap);
                Log.e("NIL",needsInfoList.toString());
//This is a mess - TA, IA values for 12 entries are all the same, they are the LAST entries of needsTitleArray and needsInfoArray on each ArrayList item.

                needsInfoAdapter = new SimpleAdapter(getBaseContext(), needsInfoList,
                        R.layout.needsinfocontent, new String[]{ "TA", "IA"},
                        new int[]{R.id.ta, R.id.ia});
                needsInfoView.setVerticalScrollBarEnabled(true);
                needsInfoView.setAdapter(needsInfoAdapter);
            }
请参阅日志行下方的注释。这就解释了我收到的输出。如何通过SimpleAdapter将ArrayList值传递给ListView中的两个文本字段

谢谢

您多次将同一HashMap实例添加到列表中,这意味着您在每次迭代中放入映射的条目将替换上一次迭代中放入的条目

您应该在每次迭代中创建一个新的HashMap实例:

for (int i = 0; i < 11; i++) {
    HashMap<String, String> needsInfoHashMap = new HashMap<>();
    needsInfoHashMap.put("TA", needsTitleArray[i]);
    needsInfoHashMap.put("IA", needsInfoArray[i]);
    needsInfoList.add(needsInfoHashMap);
    ....
}
Hashmap到ArrayList的For循环未包含正确的值

因为您正在NeedsFolist中添加相同的实例HashMap

您需要在需求列表中添加新的实例HashMap,如下代码所示

此外,您还需要将您的needsInfoAdapter设置为您的needsInfoView listview,如下代码所示

试试这个

needsInfoList = new ArrayList<>();
needsInfoView = (ListView) findViewById(R.id.needsInfo);

  for (int i = 0; i < 11; i++) {
       HashMap<String, String> needsInfoHashMap = new HashMap<>();
       needsInfoHashMap.put("TA", needsTitleArray[i]);
       needsInfoHashMap.put("IA", needsInfoArray[i]);
       needsInfoList.add(needsInfoHashMap);
   }
   needsInfoAdapter = new SimpleAdapter(getBaseContext(), needsInfoList,
                R.layout.needsinfocontent, new String[]{"TA", "IA"},
                new int[]{R.id.ta, R.id.ia});
   needsInfoView.setVerticalScrollBarEnabled(true);
   needsInfoView.setAdapter(needsInfoAdapter);

HashMap是为唯一性而设计的,如果您试图添加与previous相同的键,那么它将更新键值