如何从数组填充哈希映射

时间:2011-02-04 17:57:28

标签: java android hashmap

我有两个数组(实际上是一个,但我为每个列创建了两个)。我想用listview的值填充hashmap,但listview的所有元素都是数组的最后一个元素:

ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();

    for (int i=0; i<13; i++)
    {
        map.put("left1", date[i]);
        map.put("right1", name[i]);
        mylist.add(map);
    }


SimpleAdapter  simpleAdapter = new SimpleAdapter(this, mylist, R.layout.row,
        new String[] {"left1", "right1"}, new int[] {R.id.left, R.id.right});
lv1.setAdapter(simpleAdapter);

有什么想法吗? 感谢

1 个答案:

答案 0 :(得分:6)

您将相同的地图添加到阵列的每个插槽中。试试这个:

ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

for (int i=0; i<13; i++)
{
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("left1", date[i]);
    map.put("right1", name[i]);
    mylist.add(map);
}