android控件listview怎样显示数据库数据呢?
2013-08-30
展开全部
先创建一个帮助类BaseHelper,继承SQLiteOpenHelper,然后在获得读取的权限,BaseHelper.getReadableDatabase();查询:Cursor cursor = db.query(TABLE_NAME,str,null,null,null,null,null);,接着放到adapter里面然后就显示出来SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.showcontact, cursor, FROM, TO);
setListAdapter(adapter);。大概流程就这样,具体看帮助文档或者百度+google
setListAdapter(adapter);。大概流程就这样,具体看帮助文档或者百度+google
展开全部
使用SimpleAdapter 作为listview的适配器 通过数据的query方法 将数据放入listview:
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;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Fruit fruit = fruitList.get(position);
Toast.makeText(MainActivity.this, fruit.getName(),
Toast.LENGTH_SHORT).show();
}
});
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Fruit fruit = fruitList.get(position);
Toast.makeText(MainActivity.this, fruit.getName(),
Toast.LENGTH_SHORT).show();
}
});
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-30
展开全部
你用的什么编程工具呢?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询