无法通过SimpleCursorAdapter填充ListView

时间:2017-05-19 11:08:27

标签: android android-layout listview simplecursoradapter

我正在尝试编写一个简单的内容提供程序,并使用以下引用填充ListView:

https://developer.android.com/reference/android/app/ListActivity.html

http://www.newthinktank.com/2015/01/make-android-apps-21/

我查看了这个帖子,但这似乎不是我的问题:

SimpleCursorAdapter to populate ListView

数据库似乎工作正常,但是当我尝试绑定到我的ListView时,它会出现一个错误的列错误' _id',但我有它,因为我可以记录数据库的内容而没有问题。下面的代码片段:

记录数据库(This WORKS!):

public void logAllPatients() {
    // Projection contains the columns we want
    String[] projection = new String[]{"id", "name"};
    // Pass the URL, projection and I'll cover the other options below
    Cursor cursor = resolver.query(CONTENT_URL, projection, null, null, null);
    // Cycle through and display every row of data
    if (cursor.moveToFirst()) {
        do {
            String patientList = "";
            String id = cursor.getString(cursor.getColumnIndex("id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            patientList = patientList + id + " : " + name + "\n";
            Log.d(TEST_CONTENT_PROVIDER, patientList);
        } while (cursor.moveToNext());
    }
}

尝试填充列表视图(缺少列?,为什么)?

private void bindAllPatients() {
    try {
        // Projection contains the columns we want
        String[] projection = new String[]{"id", "name"};
        Cursor cursor = resolver.query(CONTENT_URL, projection, null, null, null);
        if (cursor != null) {
            startManagingCursor(cursor);
            cursor.moveToFirst();
            // Now create a new list adapter bound to the cursor.
            // SimpleListAdapter is designed for binding to a Cursor.
            ListAdapter adapter = new SimpleCursorAdapter(
                    this, // Context.
                    android.R.layout.two_line_list_item,
                    cursor,                                              // Pass in the cursor to bind to.
                    new String[]{"id", "name"},           // Array of cursor columns to bind to.
                    new int[]{R.id.my_id, R.id.my_name}, 0);

            // Parallel array of which template objects to bind to those columns.
            // Bind to our new adapter.
            setListAdapter(adapter);
            cursor.close();
        }
    } catch (Exception e) {
        Log.e(TEST_CONTENT_PROVIDER, e.toString());
    }
}

输出日志:

D/GMO_CONTENT_PROVIDER: 9 : Joe
D/GMO_CONTENT_PROVIDER: 10 : Mary
E/GMO_CONTENT_PROVIDER: java.lang.IllegalArgumentException: column '_id' does not exist

这里是数据库创建:

private SQLiteDatabase sqlDB;
static final String DATABASE_NAME = "myPatients";
static final String TABLE_NAME = "patients";
static final String CREATE_DB_TABLE = "CREATE TABLE " + TABLE_NAME +
        "(id INTEGER PRIMARY KEY AUTOINCREMENT, " + " name TEXT NOT NULL);";

    // bunch of code

    @Override
    public void onCreate(SQLiteDatabase sqlDB) {
        try {
            sqlDB.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            sqlDB.execSQL(CREATE_DB_TABLE);
        } catch (Exception e) {
            Log.e(TEST_CONTENT_PROVIDER, e.toString());
        }
    }

和查询覆盖,有效!

@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    queryBuilder.setTables(TABLE_NAME);
    switch (uriMatcher.match(uri)) {
        case uriCode:
            queryBuilder.setProjectionMap(values);
            break;
        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
    }
    Cursor cursor = queryBuilder.query(sqlDB, projection, selection, selectionArgs, null, null, sortOrder);
    cursor.setNotificationUri(getContext().getContentResolver(), uri);
    return cursor;
}

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

正确答案:

Android column '_id' does not exist?

“SimpleCursorAdapter要求Cursor的结果集必须包含一个名为”_id“的列。”