【发布时间】:2018-06-02 17:08:19
【问题描述】:
我有以下代码令人惊讶地不起作用;
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);
}
请查看日志行下方的评论。这解释了我收到的输出。如何使 ArrayList 值通过 SimpleAdapter 传递到 ListView 中的两个文本字段?
谢谢
【问题讨论】:
-
HashMap 是为唯一性而设计的,如果您尝试添加与 previous 相同的键,那么它将更新键值
标签: java android arraylist hashmap simpleadapter