2

Here the the description of registerContentObserver :

abstract void registerContentObserver(ContentObserver observer)

Register an observer that is called when changes happen to the content backing this cursor.

Here is my code:

Cursor cursor = (SQLiteCursor)dataBase.query(projection, null, null, null);
cursor .registerContentObserver(new ChangeObserver());

private class ChangeObserver extends ContentObserver {
    public ChangeObserver() {
        super(new Handler());
    }

    @Override
    public boolean deliverSelfNotifications() {
        return true;
    }

    @Override
    public void onChange(boolean selfChange) {
        Log.d(Constants.TAG, "ChangeObserver.onChange");
    }
}

However when I change the database(delete/update/add row), the onChange method of ContentObserver will never be called. So I want to know what's the situation will trigger the ContentObserver ?

MByD
  • 135,866
  • 28
  • 264
  • 277
manshuai
  • 803
  • 2
  • 9
  • 17
  • 2
    did you call getContext().getContentResolver().notifyChange() in your insert/update methods and Cursor.setNotificationUri() in the query() method of your ContentProvider? – Renard Apr 05 '12 at 08:06
  • No, it do nothing with content resolver. – manshuai Apr 05 '12 at 08:58

1 Answers1

6

Basically, it's a problem in the documentation, as explained in this post -- and hats off to @mikerowehl for doing the research!

It boils down to the documentation having been written mostly to describe the behavior of Cursors that work with ContentProviders/ContentResolvers. If these classes are not involved, and not manipulated as suggested by @Renard, the ContentObserver won't get a callback. Despite what the documentation says.

Hence, the raw SQLiteCursor is kind of a "poor relation" to those fancy Cursors... :)

Community
  • 1
  • 1
Hephaestus
  • 1,982
  • 3
  • 27
  • 35