我正在尝试从我的数据库中的列之一填充 ListFragment。为此,我正在尝试使用
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
db.open();
db = new NotesDatabase(getActivity());
c = db.getAllRows();
getActivity().startManagingCursor(c);
String[] fromField = new String[] { NotesDatabase.KEY_TITLE };
int[] toView = new int[] { R.id.item_title };
SimpleCursorAdapter myCAdapter = new SimpleCursorAdapter(getActivity(),
R.layout.list_item, c, fromField, toView, 0);
setListAdapter(myCAdapter);
}
但我不知道为什么我在 db.open() 上得到 NullPointerException;和 c = db.getAllRows();
db.open()
public NotesDatabase open() {
db = myDBHelper.getWritableDatabase();
return this;
}
db.getAllRows();
public Cursor getAllRows() {
String where = null;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null,
null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
数据库助手
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
或者是否有更好的方法从数据库中填充片段的列表视图?提前致谢 :)