17

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

DonGru
  • 13,532
  • 8
  • 45
  • 55
Eugene
  • 59,186
  • 91
  • 226
  • 333

6 Answers6

36

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

Pikaling
  • 8,282
  • 3
  • 27
  • 44
  • 2
    Is it always necessary to call notifyDataSetChanged() in onLoadFinished and onLoaderReset? – IgorGanapolsky Jan 23 '14 at 20:38
  • 1
    I've seen a piece of code that doesn't use notifyDataSetChanged for a SimpleCursorAdapter and a ListView. If possible, do explain when notifyDataSetChanged needs to be called and why my example case doesn't need it. – batbrat Feb 20 '14 at 08:16
  • 1
    There is no need to call `notifyDataSetChanged()` on CursorAdapter after either `changeCursor()` or `swapCursor()` calls - both methods call this method themselves, therefore your call will become just an overhead. – Vasiliy May 30 '15 at 20:21
0

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.

Jackd
  • 131
  • 1
  • 2
  • 9
0

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.

Vega
  • 27,856
  • 27
  • 95
  • 103
herrzura
  • 1
  • 4
0

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().

ASWIN S
  • 21
  • 2
0

Use BaseAdapter.notifyDataSetChanged().

xevincent
  • 3,674
  • 18
  • 20
0

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

Eugene S
  • 3,092
  • 18
  • 34
  • 3
    I think your answer is misleading - changeCursor and notifyDataSetChanged are not interchangable, they do different jobs. – Pikaling Aug 11 '11 at 13:42
  • 1
    True and one should consider which is best for the given situation, but both will "refresh" the list view. – Eugene S Aug 11 '11 at 14:02
  • So they don't need to be called together? Just calling one cancels out the need for calling the other? – IgorGanapolsky Jan 23 '14 at 20:40