【问题标题】:Adapter showing the last value in all tabs适配器显示所有选项卡中的最后一个值
【发布时间】:2024-01-17 14:09:01
【问题描述】:

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

这是我的代码:

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

}

【问题讨论】:

  • if(cursor.moveToFirst()){ do{.. condition 中创建您的HashMap Map&lt;String,String&gt; map = new HashMap&lt;String, String&gt;();
  • @PG_Android 谢谢..:)

标签: android map


【解决方案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();
        }

【讨论】:

  • 嘿,你能给我推荐一些关于安卓新实验的好书吗?
  • 有很多书籍和网站,您可以在其中学习 android,请提供您的电子邮件 ID,以便邮寄给您
  • 它的miss.grover89@gmail.com
  • 它需要 15 声望.. :(