views:

53

answers:

2

I'm trying to create a list adapter that pulls in and displays data from a database. From what I can tell, it is pulling in the info, but it fails when I try to create the ListAdapter from the new Simple cursor adapter. Not sure what I'm doing wrong here.

SQLiteDatabase db = myDbHelper.getReadableDatabase();
String select = "Select StateID, State, Details from States";            
Cursor cursor = db.rawQuery(select, null);

startManagingCursor(cursor);
ListAdapter adapter = new SimpleCursorAdapter(this,
    android.R.layout.simple_list_item_1,
    cursor,
    new String[]{"State"},
    new int[]{android.R.id.text1});

setListAdapter(adapter);

Part two will be figuring out how to assign StateID as a row id in the list so that I can access it when someone clicks on a state, but so that it's not visible.

+1  A: 

It really helps if you would use adb logcat, DDMS, or the DDMS perspective in Eclipse to look at the stack trace associated with a crash. Otherwise, we have no idea what it means when you say it "fails".

However, there is one major flaw I can see just from a code inspection: you need a column named _ID to be able to use SimpleCursorAdapter. _ID needs to be unique and a Java long (INTEGER in SQLite).

Ideally, you replace StateID with _ID. In that case, your "Part two" is solved, in that the row's ID is your _ID value. For example, when you get a long id parameter on a click on a list item, that is the _ID value.

CommonsWare
I haven't done a ton of debugging in Eclipse yet, still in the learning phase.In the LogCat I found 06-22 21:02:22.519: ERROR/AndroidRuntime(229): Uncaught handler: thread main exiting due to uncaught exception06-22 21:02:22.750: ERROR/AndroidRuntime(229): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jl.og/com.jl.og.Informational}: java.lang.IllegalArgumentException: column '_id' does not exist
AndyD273
So I renamed StateID to _id, and that seemed to do it.Which is really frustrating. It's not _ID, it has to be _id.I also have 8 tables, which I join together, and having _id as the Primary key on all the tables is a bit annoying, since they won't match the foreign keys now, which makes things goofy.Plus I use this database in several apps, which means that I have to either rewrite them to use the new structure, or I have to rename the ID columns every time I do an update... Any way to get around this?
AndyD273
Is it possible to just put 'Select StateID as _id'?
AndyD273
@AndyD273: First, sorry about the capitalization error in my reply. Yes, AFAIK, if `StateID` is an `INTEGER` column, then `SELECT StateID AS _id` should work. It's a question of what the `Cursor` has in it as the column name, not so much what is in the underlying database. If you're starting from scratch, just using `_id` for the column works. In your case, the rename may be better.
CommonsWare
Yup, StateID as _id works, which is awesome.Very Nice
AndyD273
A: 

Everything seams fine for me.

If you need to get the row id, you must have an _ID column in your query results, that will be recognized as row id.

Pentium10