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 = "Selec开发者_Python百科t 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.
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.
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.
精彩评论