[英]Android Listview displays last element in the ArrayList
嗨,我的代碼僅顯示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循環有關。 有人可以幫忙嗎! 提前致謝!
您僅創建一個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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.