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:
- Is it okay to pass
null
tonotifyChange()
as the observer parameter? If not, what object am I supposed to pass here? - Should I pass the
uri
or theitemUri
innotifyChange()
? Why? - What method do I have to call in
CommentsObserver#onChange()
to update the list of comments? - Is there no interface I could implement with the
ListFragment
instead of the inner class instance ofContentObserver
? - I simply instantiate a
new Handler()
in the constructor ofCommentsObserver
. This seems not correct to me - please explain.