Search code examples
androidandroid-layoutlistviewsimplecursoradapter

Can't get SimpleCursorAdapter to populate ListView


I am trying to write a simple content provider and populate a ListView using these references:

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

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

I looked at this thread but it does not seem to be my issue:

SimpleCursorAdapter to populate ListView

The database seems to work, but when I try to bind to my ListView it gives an error of a missing column '_id', but I have it since I can log the contents of the database without problems. Code snippets below:

Logging the database (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());
    }
}

trying to populate the listview (missing column?, why)?

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

The output log:

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

here's the database creation:

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

and the query override, which works!

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

any help is greatly appreciated!


Solution

  • The correct answer:

    Android column '_id' does not exist?

    "SimpleCursorAdapter requires that the Cursor's result set must include a column named exactly "_id"."