如何使用SQLite填充ListView?

时间:2016-10-19 11:13:11

标签: android sqlite listview android-sqlite simplecursoradapter

每次尝试运行此方法时,我的程序都会崩溃:

private void populateListView() {
    Cursor cursor = myDB.getAllRows();
    startManagingCursor(cursor);

    String[] from = new String[] {ProductContract.ProductEntry.INPUT, ProductContract.ProductEntry.DATE};
    int[] to = new int[] {R.id.t_input, R.id.t_date};

    SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.display_row, cursor, from, to, 0);
    ListView myList = (ListView) findViewById(R.id.display_listview);
    myList.setAdapter(myCursorAdapter);
}

这是我的getAllRows方法:

public Cursor getAllRows() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor c = db.query(true, ProductContract.ProductEntry.TABLE_NAME, ProductContract.ProductEntry.ALL_KEYS,
            null, null, null, null, null, null);
    if(c != null) {
        c.moveToFirst();
    }
    return c;
}

0 个答案:

没有答案