我在CursorAdapter
中使用ListFragment
来加载和显示评论列表。
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.
}
}
}
在关联的ContentProvider中,我为插入操作添加了notifyChange()
。
@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);
}
}
}
问题:
null
作为观察者参数传递给notifyChange()
?如果没有,我应该通过什么对象?uri
中的itemUri
还是notifyChange()
?为什么?CommentsObserver#onChange()
中调用哪种方法来更新评论列表?ListFragment
而不是ContentObserver
的内部类实例来实现接口?new Handler()
的构造函数中实例化CommentsObserver
。这对我来说似乎不正确 - 请解释一下。答案 0 :(得分:11)
SqlCursor
已有内部ContentObserver
,因为您正在使用此Cursor
实施,因此无需自行ContentObserver
实施。
为方便使用,您可以添加:
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
}
在此之后,CursorLoader
内部实现将负责重新加载/刷新数据......