Eugene
Eugene

Reputation: 60244

How to update ListView in case of CursorAdapter usage?

The reason I'm asking that is because requery() is deprecated. What is the best way now to refresh your ListView?

Upvotes: 17

Views: 16048

Answers (6)

ASWIN S
ASWIN S

Reputation: 21

I tried to add my response as a comment but failed for some reason.

cursoradapter.notifyDatasetchanged() should not work as your adapter is linked to a "cursor" which holds a "query" that was executed before dataset was changed. Hence one would require to change "cursor" by doing the "new query" and link to cursoradapter using cursoradapter changecursor().

Upvotes: 0

herrzura
herrzura

Reputation: 11

The only thing that helped me, was to initialise new cursor, similar as previous one like:

cursor = dbHelper.myDataBase.rawQuery(StaticValues.SQL_CAT, null);

newCursor = dbHelper.myDataBase.rawQuery(StaticValues.SQL_CAT, null);

and then call:

adapter.changeCursor(newCursor);

that updated my listview.

Upvotes: 0

Jackd
Jackd

Reputation: 131

This is what works for me, im not sure its the best way.

c = db.rawQuery( "SELECT * FROM mytable", null); //same line of the first initialization
adapter.swapCursor(c);

I refresh the only cursor, I dont know what to do with a new one. Also i dont know pepole that answer with only a name of a function.

Upvotes: 0

Pikaling
Pikaling

Reputation: 8312

requery() updates a Cursor, not a CursorAdapter. As you say, it has been deprecated, and its replacement is:

oldCursor = myCursorAdapter.swapCursor(newCursor); // hands you back oldCursor

or:

myCursorAdapter.changeCursor(newCursor); // automatically closes old Cursor

myCursorAdapter.notifyDataSetChanged() notifies the ListView that the data set has changed, and it should refresh itself

Upvotes: 37

Eugene S
Eugene S

Reputation: 3122

You can create a new cursor and call changeCursor() (documentation here) on your CursorAdapter instance or call notifyDataSetChanged() (documentation here) on your adapter.

Upvotes: 0

xevincent
xevincent

Reputation: 3734

Use BaseAdapter.notifyDataSetChanged().

Upvotes: 0

Related Questions