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.
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
_IDto be able to useSimpleCursorAdapter._IDneeds to be unique and a Javalong(INTEGERin SQLite).Ideally, you replace
StateIDwith_ID. In that case, your “Part two” is solved, in that the row’s ID is your_IDvalue. For example, when you get along idparameter on a click on a list item, that is the_IDvalue.