适配器显示所有选项卡中的最后一个值

时间:2014-09-29 06:13:45

标签: android map

我在我的应用中使用地图。一切都很好,除了适配器。我不知道它为什么只显示所有标签中的最后一个元素。

这是我的代码:

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

}

1 个答案:

答案 0 :(得分: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();
        }