【发布时间】: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 中创建您的HashMapMap<String,String> map = new HashMap<String, String>();。 -
@PG_Android 谢谢..:)