43

I have an Android ListActivity that is backed by a database Cursor through a SimpleCursorAdapter.

When the items are clicked, a flag field in the coresponding row in the database is toggled and the view in the list needs to be updated.

The problem is, when the view that's updated goes off screen and is recycled, the old value is displayed on the view when it returns into view. The same thing happens whenever thr list is redrawb (orientation changes, etc).

I use notifydatasetchanged() to refresh the cursor adapter but it seems ineffective.

How should I be updating the database so the cursor is updated as well?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
CodeFusionMobile
  • 14,812
  • 25
  • 102
  • 140

6 Answers6

90

Call requery() on the Cursor when you change data in the database that you want reflected in that Cursor (or things the Cursor populates, like a ListView via a CursorAdapter).

A Cursor is akin to an ODBC client-side cursor -- it holds all of the data represented by the query result. Hence, just because you change the data in the database, the Cursor will not know about those changes unless you refresh it via requery().


UPDATE: This whole question and set of answers should be deleted due to old age, but that's apparently impossible. Anyone seeking Android answers should bear in mind that the Android is a swiftly-moving target, and answers from 2009 are typically worse than are newer answers.

The current solution is to obtain a fresh Cursor and use either changeCursor() or swapCursor() on the CursorAdapter to affect a data change.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • So what does notifydatachanged do then? – CodeFusionMobile Dec 31 '09 at 16:58
  • 12
    There is no "notifydatachanged". If you mean notifyDataSetChanged() on Adapter, that is how the SimpleCursorAdapter tells the ListView that data was changed. To quote from the documentation, "Notifies the attached View that the underlying data has been changed and it should refresh itself." However, your problem is not with the Adapter telling the ListView about the change -- your problem is that the Adapter doesn't know the data has changed. Calling requery() is the way to address that with a CursorAdapter. – CommonsWare Dec 31 '09 at 17:03
  • That makes sense now. I misunderstood the data flow. – CodeFusionMobile Dec 31 '09 at 17:16
  • 24
    `requery()` is deprecated. You should instead issue a new `Cursor` and send it to your `CursorAdapter` via `adapter.changeCursor(myCursor)`. [See the docs](http://developer.android.com/reference/android/database/Cursor.html#requery%28%29) on the deprecation notice. – Paul Lammertsma Nov 14 '11 at 03:30
38

requery is now deprecated. from the documentation:

This method is deprecated. Don't use this. Just request a new cursor, so you can do this asynchronously and update your list view once the new cursor comes back.

after obtaining a new cursor one can use theadapter.changeCursor(cursor). this should update the view.

rony l
  • 5,798
  • 5
  • 35
  • 56
  • Thanks for the info, I've been having a crash on Honeycomb related to cursor.requery(), maybe this is behind it. – CodeFusionMobile Jun 15 '11 at 14:09
  • Common situation is to change value from onClickListener() What puzzles me is that on the one hand, we are sending initial cursor through constructor ( i.e. query is written outside the Adpater) But on the other hand, the onlickLister is deifned as nested class inside the adapter. So the query for new cursor would be written inside the adapter class. This may cause duplicate code. What is the right design to write query only once in the source code.? – Frederic Bazin Sep 25 '12 at 02:15
21

In case of using loader and automagically generated cursor you can call:

getLoaderManager().restartLoader(0, null, this);

in your activity, just after changing something on a DB, to regenerate new cursor. Don't forget to also have event handlers defined:

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    CursorLoader cursorLoader =
            new CursorLoader(this,
                    YOUR_URI,
                    YOUR_PROJECTION, null, null, null);
    return cursorLoader;
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    adapter.swapCursor(data);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
    adapter.swapCursor(null);
}
siefca
  • 1,007
  • 13
  • 16
  • 2
    Tried half a dozen different things from around Stack Overflow and this was the one that finally worked for me. Thank you! – Rudism May 02 '13 at 01:36
  • 1
    This is exactly the answer I was looking for, using a simple adapter, and calling swapCursor onLoadFinished. – Matt Dec 27 '13 at 15:48
  • 1
    Worked, but I had to add this: As the listview Fragment registers a user click causing an update of values of a row that is then to be inserted into DB, the new cursor automagically created by the new cursorloader in onCreateLoader(), had to be passed to the (in my case) custom-adapter extending simpleCursorAdapter, because I found no other way to notify the method getView() (doing the view update) about the new cursor. GetView() used the old cursor until the whole fragment was restarted. Hence I made a setCursor() in the customadapter and called this from onCreateLoader(). Then it worked. – carl Apr 14 '14 at 18:11
1

I am not clear if you set the autoRequery property of CursorAdapter to true.

The adapter will check the autoRequery property; if it is false, then the cursor will not be changed.

apaderno
  • 28,547
  • 16
  • 75
  • 90
gjican
  • 11
  • 1
  • `protected void init (Context context, Cursor c, boolean autoRequery)` *This method is deprecated. Don't use this, use the normal constructor. This will be removed in the future.* – Mussa May 27 '15 at 06:27
0

requery() is already deprecated, just implement the simple updateUI() method like this in your CursorAdapter's child class and call it after data updates:

private void updateUI(){
    swapCursor(dbHelper.getCursor());
    notifyDataSetChanged();
}
Jaroslav
  • 263
  • 4
  • 6
0

It's easy.

private Db mDbAdapter;
private Cursor mCursor;
private SimpleCursorAdapter mCursorAd;

.....................................
//After removing the item from the DB, use this
.....................................

 mCursor = mDbAdapter.getAllItems();
 mCursorAd.swapCursor(mCursor);

Or use CursorLoader...

Shwarz Andrei
  • 647
  • 14
  • 17