android如何将从数据库读取的数据显示在listview中?
1个回答
展开全部
final SimpleAdapter adapter = new SimpleAdapter(this, getData(),
R.layout.mainlayout, new String[] { "text", "time", "image" },
new int[] { R.id.PL_TextView01, R.id.PL_TextView02,
R.id.PL_ImageView01 });
lv.setAdapter(adapter);
private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
Cursor cur = DR.query("DB", new String[] { "text", "time" }, null,
null, null, null, "time desc");
while (cur.moveToNext()) {
for (int i = 0; i < cur.getCount(); i++) {
cur.moveToPosition(i);
String text = cur.getString(0);
String time = cur.getString(1);
map = new HashMap<String, Object>();
map.put("text", text);
map.put("time", time);
map.put("image", R.drawable.ic_menu_close_clear_cancel);
list.add(map);
}
}
return list;
}
我是使用SimpleAdapter 作为listview的适配器 通过数据的query方法 将数据放入listview
R.layout.mainlayout, new String[] { "text", "time", "image" },
new int[] { R.id.PL_TextView01, R.id.PL_TextView02,
R.id.PL_ImageView01 });
lv.setAdapter(adapter);
private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
Cursor cur = DR.query("DB", new String[] { "text", "time" }, null,
null, null, null, "time desc");
while (cur.moveToNext()) {
for (int i = 0; i < cur.getCount(); i++) {
cur.moveToPosition(i);
String text = cur.getString(0);
String time = cur.getString(1);
map = new HashMap<String, Object>();
map.put("text", text);
map.put("time", time);
map.put("image", R.drawable.ic_menu_close_clear_cancel);
list.add(map);
}
}
return list;
}
我是使用SimpleAdapter 作为listview的适配器 通过数据的query方法 将数据放入listview
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询