1

嗨,我的代码只显示了 ArrayList 的最后一个元素!这是我的代码:

    ListView list = (ListView) findViewById(R.id.t1player_statsList);

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

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

    for(Player player : team1.getPlayers())
        map.put("player", player.getName()); 
        map.put("score", "0");
        mylist.add(map);


    SimpleAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.mylistrow,
                new String[] {"player", "score"}, new int[] {R.id.NameCell, R.id.ScoreCell});
    list.setAdapter(mSchedule);

我觉得这与我的 for 循环有关。有人可以帮忙!!提前致谢!

4

1 回答 1

6

您只创建了一个 Hashmap 实例,它一次又一次地覆盖所有值,最后一个值仍然存在。因此,在 for 循环中创建一个新实例。

    HashMap<String, String> map;

    for(Player player : team1.getPlayers()){
        map = new HashMap<String, String>();
        map.put("player", player.getName()); 
        map.put("score", "0");
        mylist.add(map);
     }
于 2012-05-19T11:53:29.253 回答