2012-05-19 15 views
1

안녕하세요 제 코드는 ArrayList의 마지막 요소 만 보여줍니다! Heres는 내 코드 :안드로이드리스트 뷰는 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); 

내가 그 무언가가 내 루프와 함께 할 생각합니다. 누군가 도와주세요! 미리 감사드립니다!

답변

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

+1 멋진 캐치 ... –

+0

고맙습니다. 그것은 일했다! –

+1

굉장! 이것은 매력처럼 작동했습니다. 거의 하루 종일 디버깅 중이 었어. 고마워. @Lalit Poptani. – buggydroid

관련 문제