How to implement a ContentObserver in a fragment using CursorAdapter and LoaderCallbacks?
J

1

5

I am using a CursorAdapter in a ListFragment to load and display a list of comments.

public class CommentsFragment extends ListFragment implements LoaderCallbacks<Cursor> {

    protected Activity mActivity;
    protected CursorAdapter mAdapter;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mActivity = getActivity();
        mAdapter = new CommentsCursorAdapter(mActivity, null, 0);
        setListAdapter(mAdapter);
        mActivity.getContentResolver().registerContentObserver(CustomContract.Comments.CONTENT_URI, false, new CommentsObserver());
        getLoaderManager().initLoader(0, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle extras) {
        Uri uri = CustomContract.Comments.CONTENT_URI;
        return new CursorLoader(mActivity, uri, null, null, null, null);
    }

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

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        mAdapter.swapCursor(null);
    }

    protected class CommentsObserver extends ContentObserver {

        public CommentsObserver() {
            super(new Handler());
        }

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            // TODO Trigger a reload.
        }

    }
}

In the associated ContentProvider I added notifyChange() for the insert action.

@Override
public Uri insert(Uri uri, ContentValues values) {
    final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    int match = sUriMatcher.match(uri);
    switch (match) {
        case COMMENTS: {
            long id = db.insert(DatabaseProperties.TABLE_NAME_COMMENTS, null, values);
            Uri itemUri = ContentUris.withAppendedId(uri, id);
            // FIXME Which one is right?
            getContext().getContentResolver().notifyChange(itemUri, null);
            getContext().getContentResolver().notifyChange(uri, null);
            return itemUri;
        }
        default: {
            throw new UnsupportedOperationException("Unknown URI: " + uri);
        }
    }
}

Questions:

  1. Is it okay to pass null to notifyChange() as the observer parameter? If not, what object am I supposed to pass here?
  2. Should I pass the uri or the itemUri in notifyChange()? Why?
  3. What method do I have to call in CommentsObserver#onChange() to update the list of comments?
  4. Is there no interface I could implement with the ListFragment instead of the inner class instance of ContentObserver?
  5. I simply instantiate a new Handler() in the constructor of CommentsObserver. This seems not correct to me - please explain.
Jennifer answered 16/8, 2012 at 9:51 Comment(3)
ok, i did it ... you can try my library selvinlistsyncsample.codeplex.com ... yes, it's intended to use it with MSFT ... but it can also works without it ... you can remove all sync code and then you will have library for easy runtime ContentProvider generator from Annotations :)Eumenides
I will have a look at it. Thank you, again.Jennifer
check this ... i've stripped sync code and made sample projectEumenides
E
10

SqlCursor already have internal ContentObserver and since you're using this Cursor implementation there is no need for own ContentObserver implementation.

For easy use you can add:

class MyContentProvider extends ContentProvider{
  //rest implementation goes here
  @Override
  public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
    Cursor cursor = null;
    //check uri, select table name based on uri, etc ...
    cursor = db.query(/*.....*/);
    //add this line:
    cursor.setNotificationUri(getContext().getContentResolver(), uri);
    //before you return cursor
    return cursor;
  }
  @Override
  public Uri insert(Uri uri, ContentValues values) {
     /* ... */
       //you can notify only "dir" uri and is should be enough
       getContext().getContentResolver().notifyChange(uri, null);
       return itemUri;
     /* ... */
  }
  //rest of implementation goes here
}

after this, CursorLoader internal implementation will take care of reloading/refreshing data ...

Eumenides answered 16/8, 2012 at 11:29 Comment(5)
I am having a similar problem and I've implemented my ListFragment without a ContentObserver and my ListView doesn't update. Currently when the fragment is created it starts and intent service to query the server for updated data while showing the old data from the local SQLLite DB. When retrieved it updates the local DB and notifies the change. In my first implementation with the ContentObserver onChange fires and I invoke notifyDataSetChanged on the SimpleCursorAdapter. I tried the above design too and still doesn't update. When I recreate the activity and fragment it shows the new data.Iodometry
are you using CursorLoader? and do you use update method of CP in your retrive from cloud code?Eumenides
I am using cursor loader and in my service connecting to the cloud it does an insert, "getContentResolver().insert(Uri.withAppendedPath(MyProvider.CONTENT_URI, "table name"), values);" and the execution of that insertsWithOnConflict based on a unique filed and does an update. So typically it does an update.Iodometry
are you sure that uris are the same (in insert and in "select"(new CursorLoader(..., uri, ...) and getContentResolver().insert(uri,...)), next did you add those line which i mention in my answer (notifyChange in insert/update and setNotificationUri in select) ...for checking if "autorefresh" mechanism works, add button/menu with code getContentResolver().notifyChange(uri, null); to your ListFragment ... i'm pretty sure that it should works ... anyway check code from my second comment to questionEumenides
after I double checked the URIs the notification to the URI on the CP different. Notifying after insert in the service solved my problem. Thanks for the clues!Iodometry

© 2022 - 2024 — McMap. All rights reserved.