Question

So yeah, I've seen questions about this all over the place, and so have accordingly added an _id alias to my query as below:

        SimpleCursorAdapter sca = new SimpleCursorAdapter(getActivity(),
            R.layout.activity_contact_list, cursor, new String[] {
                    "rowid _id", DBOps.COL_CATNAME },
            new int[] { R.id.contact_list }, CursorAdapter.NO_SELECTION);

I'm creating my cursor like so:

    public Cursor getAllCategories() {
    return mDB.query(TABLE_NAME, new String[] { "rowid _id", COL_ID,
            COL_CATNAME,
            COL_ICONPATH }, null, null, null, null, null);
}

mDB in the above is a SQLite database. I've tried changing the string to rowid as _id, which also doesn't work. Also apparently there's no need to change my table structure by adding another _id column as a few others have noted, so where am I going wrong here?

Update - here's the stack trace -

Caused by: java.lang.IllegalArgumentException: column 'rowid _id' does not exist
at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
at android.widget.SimpleCursorAdapter.findColumns(SimpleCursorAdapter.java:333)
at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:107)
at com.rex.bizcontacts.ContactListFragment.onCreate(ContactListFragment.java:77)
Was it helpful?

Solution

No, you're not getting the problem.

The comment you are complaining about has the right idea. rowid _id is not a valid name. You can tell that by looking at the exception.

You are welcome to try "rowid AS _id" instead of "rowid _id". I would recommend rawQuery() rather than query(), so this can be written more naturally ("SELECT rowid AS _id, ...").

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top