spryan spryan - 1 year ago 294
Android Question

android: Refresh ListView using ListAdapter and SimpleCursorAdapter

I'm trying to refresh a ListView that uses a ListAdapter created as a SimpleCursorAdapter.

Here is my code for creating the Cursor and ListAdapter in onCreate which populates the ListView.

tCursor = db.getAllEntries();

ListAdapter adapter=new SimpleCursorAdapter(this,
R.layout.row, tCursor,
new String[] columns,
new int[] {R.id.rowid, R.id.date});


Then, I add some data to the db in another method, but I can't figure out how to refresh the ListView. Similar questions on stackoverflow and other places mention using notifyDataSetChanged() and requery(), but neither are methods of ListAdapter or SimpleCursorAdapter.

Answer Source

I'm able to get the ListView to refresh by creating a new adapter and calling setListAdapter again.

I named it adapter2 in the other method.

tCursor = db.updateQuery();       

ListAdapter adapter2=new SimpleCursorAdapter(this,
                R.layout.row, tCursor,
                new int[] {R.id.rowid, R.id.date});


I'm not sure why this is necessary, but it works for now. If anyone has a better solution, I'm willing to try it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download