views:

111

answers:

1

I'm having trouble with something that works in the Notepad example. Here's the code from the NotepadCodeLab/Notepadv1Solution:

String[] from = new String[] { NotesDbAdapter.KEY_TITLE }; int[] to = new int[] { R.id.text1 };

SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);

This code seems to work fine. But just to be clear, I ran the adb utility and run sqlite3 I inspected the schema as follows:

sqlite> .schema CREATE TABLE android_metadata (locale TEXT); CREATE TABLE notes (_id integer primary key autoincrement, title text not null, body text not null);

All seems good to me.


Now on to My App, which as far as I can see is basically the same with a few minor changes. I've simplified and simplified my code, but the problem persists.

String[] from = new String[] { "x" }; int[] to = new int[] { R.id.x };

SimpleCursorAdapter adapter = null; try { adapter = new SimpleCursorAdapter(this, R.layout.circle_row, cursor, from, to); } catch (RuntimeException e) { Log.e("Circle", e.toString(), e);

}

When I run my app, I get a RuntimeException and the following prints in LogCat from my Log.e() statement:

LogCat Message: java.lang.IllegalArgumentException: column '_id' does not exist

So, back to sqlite3 to see what's different about my schema:

sqlite> .schema CREATE TABLE android_metadata (locale TEXT); CREATE TABLE circles (_id integer primary key autoincrement, sequence integer, radius real, x real, y real);

I don't see how I'm missing the '_id'.

Can anyone point out what I've done wrong?

One thing that's different between my app and the Notepad example is that I started by creating my application from scratch using the Eclipse wizard while the sample app comes already put together. Is there some sort of environmental change I need to make for a new app to use a sqlite database?

Hopefully this is easy to spot by someone that's not a newbie like me. Thanks.

A: 

I see, the documentation for CursorAdapter states:

The Cursor must include a column named "_id" or this class will not work.

The SimpleCursorAdapter is a derived class, so it appears this statement applies.

However, the statement is technically wrong and somewhat misleading to a newbie. The result set for the cursor must contain "_id", not the cursor itself. I'm sure this is clear to a DBA because that sort of shorthand documentation is clear to them, but for those newbies, being incomplete in the statement causes confusion. Cursors are like iterators or pointers, they contain nothing but a mechanism for transversing the data, they contain no columns themselves.

I recommend the statement in the documentation be written as "The Cursor's result set must include a column named exactly "_id". Failing to do so will produce a RuntimeException." This would be less confusing and correct.