android: Refresh ListView using ListAdapter and SimpleCursorAdapter
11,680
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,
columns,
new int[] {R.id.rowid, R.id.date});
setListAdapter(adapter2);
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.
Author by
spryan
Updated on June 23, 2022Comments
-
spryan almost 2 years
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}); setListAdapter(adapter);
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.
-
spryan about 13 yearsAs I mentioned in the original question, notifyDataSetChanged() does not work for a ListAdapter. The error given by Eclipse says "The method notifyDataSetChanged() is undefined for the type ListAdapter".
-
spryan about 13 yearsRewriting code that works is hopefully a last resort, but I'll take that into consideration. Thanks for trying.
-
spryan about 13 yearsI changed the type of adapter from ListAdapter to BaseAdapter as you suggested. The list still loads properly, but calling adapter.notifyDataSetChanged() doesn't refresh the ListView when I call it from another method. Am I using this incorrectly?
-
Kalimah over 12 yearsSolved my problem too. I can use this in AsynTask as well.