0

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)
Rex
  • 801
  • 1
  • 17
  • 26
  • 1
    please share the exception. – Shahar Dec 23 '13 at 11:53
  • Yep, I've added it now. – Rex Dec 23 '13 at 12:11
  • first, 'rowid _id' is not a valid name... maybe 'rowid_id'. and COL_ID = ?? – Shahar Dec 23 '13 at 12:23
  • No, you're not getting the problem. Read the documentation for CursorAdapter where it says you must include an automatically used column called '_id' for it to work. (Or take a look at the other questions similar to this one). If you don't use an alias for _id, it will complain about it even though there isn't such a column to start with. – Rex Dec 23 '13 at 12:28
  • This may help: http://stackoverflow.com/questions/3359414/android-column-id-does-not-exist – ivan.aguirre Dec 23 '13 at 12:38
  • As mentioned in my question, I've already seen all these. I'm using the same syntax and it's not working, so what else has gone wrong? – Rex Dec 23 '13 at 12:41
  • you are not using the same syntax, you send a query for rowid of type _id... remove the rowid, send only '_id' and then show us the exception if any.. – Shahar Dec 23 '13 at 12:49

1 Answers1

3

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, ...").

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I've done both, and each time it continues to complain with the same error. – Rex Dec 23 '13 at 12:43
  • Let me try with rawQuery..however the problem is with the SimpleCursorAdapter call, which doesn't take a query string. – Rex Dec 23 '13 at 12:51
  • @Rex: `AS` is the appropriate syntax, and I know that it works with `rawQuery()`: https://github.com/commonsguy/cw-omnibus/tree/master/Database/Constants https://github.com/commonsguy/cw-omnibus/tree/master/Database/ConstantsROWID – CommonsWare Dec 23 '13 at 12:52
  • @Rex: No, the problem is with the `Cursor`. – CommonsWare Dec 23 '13 at 12:52
  • Ok, I use 'rowid AS _id' (case sensitive) in the Cursor and it still complains that the column doesn't exist. I switch to a query() from a rawQuery() (what I'd prefer), and there it expects an actual column name, so just passing _id won't work. 'rowid AS _id' is invalid syntax. – Rex Dec 23 '13 at 13:13
  • 1
    @Rex: See the samples that I linked to. – CommonsWare Dec 23 '13 at 13:26
  • Turns out I was using the row alias in the cursor used in SimpleCursorAdapter's constructor..removing that fixed it. – Rex Dec 23 '13 at 14:37