HashMap值未附加到ListView
我正在尝试从具有1个键的多个值的散列映射中检索数据并将其设置为列表视图,但不是将值设置到列表视图中并显示列表视图,而是显示的所有内容都是数组(没有键)。 代码如下:
ListView lv = (ListView)findViewById(R.id.list); //hashmap of type `HashMap<String, List>` HashMap<String, List> hm = new HashMap<String, List>(); List values = new ArrayList(); for (int i = 0; i < j; i++) { values.add(value1); values.add(value2); hm.put(key, values); }
并检索值并放入列表视图
ListAdapter adapter = new SimpleAdapter( MainActivitty.this, Arrays.asList(hm), R.layout.list_item, new String[] { key, value1,value2}, new int[] { R.id.id, R.id.value1,R.id.value2 }); // updating listview lv.setAdapter(adapter);
例如,key = 1,value2 = 2和value3 = 3,它将显示一个看起来像[2,3]的数组。 如何让它显示lisview并添加密钥呢?
SimpleAdapters Consturctor表示它是第二个参数:
数据:地图列表。 列表中的每个条目对应于列表中的一行。 地图包含每行的数据,并应包含“from”中指定的所有条目
但是HashMap
hm是列表的映射。 所以像List
将是您可能需要的数据类型。
这是编辑过的来源:
ListView lv = (ListView)findViewById(R.id.list); List
我不明白为什么你想要它的密钥(如果你需要更多的话,只需将字符串添加到地图中)?