I am using a database, getting a cursor out of it and then using a simplecursoradapter to populate a listview. I can't seem to figure out why the app crashes on creating a new simplecursoradapter. My cursor is contained in a singleton object. I have a reference to it through the data variable in this class.
String[] columns = new String[] {"item", "quantity", "days"};
int[] to = new int[] { R.id.nametext, R.id.quantitytext, R.id.dayslefttext};
SimpleCursorAdapter sCA = new SimpleCursorAdapter(this, R.layout.itemrow, data.listCursor, columns, to);
listView1.setAdapter(sCA);
What does the simplecursoradapter do with the information passed to it in its constructor? Is one of the parameters in the wrong format? Here is the simple xml file for each row that I am using to just see if this works:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Loading Screen"
android:id="@+id/nametext"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Loading Screen"
android:id="@+id/quantitytext"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Loading Screen"
android:id="@+id/dayslefttext"/>
</LinearLayout>
Here is how I load the cursors:
listCursor = myDataBase.query(ITEMS_TABLE, null, "location" +" = ?", new String[]{IN_SPECIAL_LIST}, null, null, "item");
I can't figure out what the problems is. I ran this through some debugging and the problem occurs when I I create the new simplecursoradapter.
myDataBase.query(ITEMS_TABLE, null, ...
this null is your problem, it means that query will use SELECT *
but there is no column _id
in your table so you should use
new String[] {"YOURID AS _id", "item", "quantity", "days"}
SimpleCursorAdapter need _id column
YOURID could be ROWID (ROWID as _id) or any int or long ID from your DB
来源:https://stackoverflow.com/questions/5536795/simplecursoradapter-not-working