I have two other apps that use a ListView and SimpleCursorAdapter. The code is almost the exact same. So I decided to use it again on a third app. It is not working, and cannot find a solution.
The problem is that there is nothing displayed in the list. I use a button to add information into the database. I was logging a count on the cursor Log.d("",storyCur.count()+""); to make sure the database was being updated and it is.
Relevant Database Code:
public Cursor fetchAll(){
return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID,KEY_DETAILS,KEY_TITLE},
null, null, null, null, null);
}
Rest of relevant code:
ListView lv;
DbAdapter db;
public void onCreate(Bundle savedInstanceState) {
...
lv = (ListView) findViewById(R.id.list);
db = new DbAdapter(this);
....
}
public void onResume(){
super.onResume();
db.open();
fillData();
}
private void fillData(){
Cursor storyCur = db.fetchAll();
startManagingCursor(storyCur);
String[] from = new String[]{DbAdapter.KEY_LOC};
int[] to = new int[]{R.id.row1};
SimpleCursorAdapter stories =
newSimpleCursorAdapter(this,R.layout.story_row,storyCur,from,to);
lv.setAdapter(stories);
storyCur.close();
}
main XML:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="onClick" />
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
story_row XML:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/row1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:background="#444444" />
You should remove the call to .close() on the cursor. SimpleCursorAdapter needs the cursor to be open in order to function and will call close() on the cursor for you.