【问题标题】:Can't get SimpleCursorAdapter to populate ListView无法让 SimpleCursorAdapter 填充 ListView
【发布时间】:2017-05-19 11:08:27
【问题描述】:

我正在尝试编写一个简单的内容提供程序并使用以下引用填充 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”的错误,但我有它,因为我可以毫无问题地记录数据库的内容。代码如下:sn-ps:

记录数据库(这工作!):

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;
}

非常感谢任何帮助!

【问题讨论】:

  • startManagingCursor(cursor); 在做什么?确保在使用后关闭光标。
  • @MuraliPrajapati 当然你不能在 close() 使用光标时使用 CursorAdapter
  • @pskink 我的意思是在startManagingCursor(cursor); 方法中关闭光标。
  • 您可以尝试在您的数据库合同和预测中将列名从“id”更改为“_id”吗?
  • @Srichakradhar 这是正确的。谢谢

标签: android android-layout listview simplecursoradapter


【解决方案1】:

正确答案:

Android column '_id' does not exist?

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-11
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多