CursorLoader通知数据更改 [英] CursorLoader notify data change

查看:102
本文介绍了CursorLoader通知数据更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了自定义的 ContentProvider ,它具有几个 URI :

I've implemented my custom ContentProvider and it has several URIs:

主要的:

//Return all items, uriType = ALLITEMS
String BASEURI = "content://authority/items"

//Return all items in category #, uriType = ITEMS
"content://authority/items/cat/#" 
//Return all items in category # starting with *, uriType = ITEMS_INITIAL
"content://authority/items/cat/#/*"

我的 Activity 实现了这些 Loader 回调:

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle arg1) {
    CursorLoader mCursorLoader = null;
    switch (id) {
    case 0:
        mCursorLoader = new CursorLoader(
                mActivity,
                Uri.parse("content://authority/items/cat/"
                        +mCurrentID), mColumns, null, null, null);
        break;
    }
    return mCursorLoader;
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    switch (loader.getId()) {
    case 0:
        cursor.setNotificationUri(getContentResolver(),MyContentProvider.BASEURI);
        if (null == mAdapter)
            mAdapter = new GridViewCursorAdapter(this, cursor,0);
        //gv is a GridView
        if (gv.getAdapter() != mAdapter)
            gv.setAdapter(mAdapter);
        if (mAdapter.getCursor() != cursor)
            mAdapter.swapCursor(cursor);
        break;
    }
}

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

当我要插入数据时,我使用:

When I want to insert data I use:

for (ItemClass item : itemsToInsert) {
    getContentResolver().insert(MyContentProvider.BASEURI, itemToContentValues(item));
}

最后定义了 MyContentProvider 中的 insert 方法:

@Override
public Uri insert(Uri uri, ContentValues values) {
    SQLiteDatabase database = db.getWritableDatabase();
    int turiType = sURIMatcher.match(uri);
    long id = 0;
    switch (uriType) {
    case ALLITEMS:
        id = database.insert(MySQLiteHelper.TABLE_ITEMS, null, values);
        break;
    default:
        throw new IllegalArgumentException("Unknown URI (" + uri + ")");
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return null;//I will implement uri path to single item later
}

如您所见,用于初始化 Loader 的默认URI是按类别ID进行过滤的过滤器,而不是 BASEURI 的过滤器,但是我使用了 Cursor.setNotificationUri 将通知URI设置为 BASEURI ,但是我的 GridView 的内容未更新.

As you can see, the default URI used to initialize the Loader is the filter by category id and not the BASEURI one, but I used Cursor.setNotificationUri to set the notification URI to BASEURI, but the content of my GridView isn't updated.

如果我重新启动 Activity ,我可以看到插入的数据,因此只是通知不起作用.我该如何更改才能正确通知装载机?

If I restart the Activity I can see the inserted data, so it's just the notification that doesn't work. What should I change to get the loader notified properly?

推荐答案

ContentProvider (和任何数据库)中进行过滤的最佳方法是使用 where 声明.在这种情况下,这意味着您的 CursorLoader 类似于:

The best way to do filtering in a ContentProvider (and any database), is by using a where statement. In this case, that would mean your CursorLoader would look something like:

mCursorLoader = new CursorLoader(
  mActivity,
  MyContentProvider.BASEURI,
  mColumns, 
  MyContentProviderColumns.CATEGORY + "=?", // category = ?
  new String[] { Integer.toString(mCurrentID) }, // pass in your category
  null);

传递选择,过滤在您所调用的类别列上的所有内容,并且选择args是类别值本身.在这种情况下,您只有一个URI,因此更改通知更易于管理.

Passing in the selection, filtering on whatever your category column is called, and the selection args being the category value itself. In this case, you only have one URI, so notifications of changes are much easier to manage.

如果确实需要单独的URI,另一种选择是在 insert 语句中进行多个 notify 调用:

If you really, really need separate URIs, the other option is to do multiple notify calls in your insert statement:

getContext().getContentResolver().notifyChange(uri, null);

int category = extractCategoryFromContentValues(values);
getContext().getContentResolver().notifyChange("content://authority/items/cat/"
                    + category, null);

这篇关于CursorLoader通知数据更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆