2014-09-29 47 views
2

我在我的應用程序中使用地圖。一切都很好,除了適配器。我不知道爲什麼它只顯示所有選項卡中的最後一個元素。適配器顯示所有選項卡中的最後一個值

這裏是我的代碼:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

     List<Map<String,String>> list = new ArrayList<Map<String,String >>(); 

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


     EmployeeDatabase empClick = new EmployeeDatabase(getApplicationContext()); 
     Cursor cursor = empClick.getDetails(); 
     if(cursor.moveToFirst()){ 
      do{ 
       String name = cursor.getString(cursor.getColumnIndex("name")); 
       String age = cursor.getString(cursor.getColumnIndex("age")); 
       map.put("name",name); 
       map.put("age",age); 
       list.add(map); 
      }while(cursor.moveToNext()); 
      cursor.close(); 
     } 


     SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.show_data, new String [] {"name", "age"}, new int[] {R.id.name,R.id.age}); 
     setListAdapter(adapter); 

} 
+0

創建''HashMap'地圖地圖=新的HashMap ();''裏面如果(cursor.moveToFirst ()){{...}條件。 – Piyush 2014-09-29 06:19:34

+0

@PG_Android謝謝.. :) – Sangeeta 2014-09-29 07:27:56

回答

1
if(cursor.moveToFirst()){ 
      do{ 
       map = new HashMap<String, String>();<-------Add this line 
       String name = cursor.getString(cursor.getColumnIndex("name")); 
       String age = cursor.getString(cursor.getColumnIndex("age")); 
       map.put("name",name); 
       map.put("age",age); 
       list.add(map); 
      }while(cursor.moveToNext()); 
      cursor.close(); 
     } 
+0

非常感謝你.. :) – Sangeeta 2014-09-29 07:10:23

+0

如果這項工作比PLZ upvode和標記爲正確 – 2014-09-29 07:11:09

+0

嘿你能建議我一些好的書在Android的新實驗嗎? – Sangeeta 2014-09-29 07:15:58

相關問題