gpt4 book ai didi

java - 如何从数组填充 HashMap

转载 作者:行者123 更新时间:2023-11-29 07:20:50 24 4
gpt4 key购买 nike

我有两个数组(实际上是一个,但我为每一列创建了两个)。我想用 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);

有什么想法吗?谢谢

最佳答案

您要将相同的 map 添加到数组的每个槽中。试试这个:

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);
}

关于java - 如何从数组填充 HashMap ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4901281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com