我正在尝试从具有1个键的多个值的hashmap中检索数据并将其设置为listview,而不是设置值 进入列表视图并显示列表视图,显示的只是数组(没有键)。 代码如下:
ListView lv = (ListView)findViewById(R.id.list);
//hashmap of type `HashMap<String, List<String>>`
HashMap<String, List<String>> hm = new HashMap<String, List<String>>();
List<String> values = new ArrayList<String>();
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并添加密钥呢?
答案 0 :(得分:1)
SimpleAdapters Consturctor表示它是第二个参数:
数据:地图列表。列表中的每个条目对应于一行 列表。地图包含每行的数据,应包括 “from”
中指定的所有条目
但HashMap<String, List<String>>
hm是列表地图。因此List<Map<String,String>> hm
就像你可能需要的数据类型一样。
以下是经过编辑的来源:
ListView lv = (ListView)findViewById(R.id.list);
List<Map<String,String>> mapList = new ArrayList<Map<String, String>>();
Map<String,String> mapPerRow;
for (int i = 0; i < rowNumbers; i++) {
mapPerRow = new HashMap<String, String>();
mapPerRow.put("column1", value1);
mapPerRow.put("column2", value2);
mapList.add(mapPerRow);
}
ListAdapter adapter = new SimpleAdapter(
MainActivitty.this, mapList,
R.layout.list_item, new String[] { "column1", "colum2"},
new int[] { R.id.value1,R.id.value2 });
// updating listview
lv.setAdapter(adapter);
我不明白为什么你想要密钥(如果你需要更多的话,只需将字符串添加到地图中)?