如何使用CursorAdapter和LoaderCallbacks在片段中实现ContentObserver?

时间:2012-08-16 09:51:30

标签: android android-contentprovider android-cursoradapter android-cursorloader contentobserver

我在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);
        }
    }
}

问题:

  1. 是否可以将null作为观察者参数传递给notifyChange()?如果没有,我应该通过什么对象?
  2. 我应该通过uri中的itemUri还是notifyChange()?为什么?
  3. 我需要在CommentsObserver#onChange()中调用哪种方法来更新评论列表?
  4. 我是否可以使用ListFragment而不是ContentObserver的内部类实例来实现接口?
  5. 我只是在new Handler()的构造函数中实例化CommentsObserver。这对我来说似乎不正确 - 请解释一下。

1 个答案:

答案 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内部实现将负责重新加载/刷新数据......