创建表的属性id必须是 _id
将数据库表中值直接作为listview item中控件的数据源
使用:
Cursor curosr = sqliteDb.query(Table_name,null,null,null,null,null,null;);
MyCursorAdapter adapter = new MyCursorAdapter(getContext,cursor);
mListView.setAdapter(adapter);
public class MyCursorAdapter extends CursorAdapter {
private LayoutInflater infalter;
public MyCursorAdapter(Context context, Cursor c) {
super(context, c, true);
infalter = LayoutInflater.from(context);
}
/*
* 返回的就是子布局
*/
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return infalter.inflate(R.layout.item_list_mine_layout, null);
}
/*
* 将cursor中的值取出来付给子布局里面的控件
*/
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView title = view.findViewById(R.id.cursor_title_txt);
TextView content = view.findViewById(R.id.cursor_content_txt);
title.setText(cursor.getString(cursor.getColumnIndex("title")));
content.setText(cursor.getString(cursor.getColumnIndex("content")));
}
}