ContentObserver 获取有关删除的通知,但不获取有关插入或更新的通知

发布于 2024-11-02 13:37:23 字数 1009 浏览 2 评论 0原文

我的活动中有以下代码:

Cursor mCursor = managedQuery(ActivityColumns.CONTENT_URI, PROJECTION, null, null,
                getSortOrderStringFromSpinner());

        mCursor.registerContentObserver(new ContentObserver(new Handler())
        {
            @Override
            public void onChange(boolean selfChange)
            {
                ...snip...// breakpoint is set here                 
            }

            @Override
            public boolean deliverSelfNotifications()
            {
                return true;
            }
        });

...并且在我用来获取数据的 DataProvider 中,我将查询方法中的通知 URI 设置为我正在查询的类型的 uri:

c.setNotificationUri(getContext().getContentResolver(), uri);

最后,在更新中,插入和删除,如果操作成功,则会通知订阅者:

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

上面一行中的 notificationUri 是正在插入/更新或删除的项目的 URI。

通过在“...snip...”代码行上设置断点,我添加、删除和更新一些记录。我只会收到有关删除的通知,但不会收到更新或插入的通知。我做错了什么?

I have the following code in my activity:

Cursor mCursor = managedQuery(ActivityColumns.CONTENT_URI, PROJECTION, null, null,
                getSortOrderStringFromSpinner());

        mCursor.registerContentObserver(new ContentObserver(new Handler())
        {
            @Override
            public void onChange(boolean selfChange)
            {
                ...snip...// breakpoint is set here                 
            }

            @Override
            public boolean deliverSelfNotifications()
            {
                return true;
            }
        });

...and in the DataProvider I am using to get my data, I am setting the notification URI in the query method to the uri of the type I am querying for:

c.setNotificationUri(getContext().getContentResolver(), uri);

Finally, in update, insert and delete, if the operation is successful, the subscribers are notified:

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

The notificationUri in the line above is the URI of the item being inserted/updated or deleted.

With a breakpoint set on the "...snip..." line of code, I add, remove and update some records. I ONLY get notified about the deletes, but not updates or inserts. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深陷 2024-11-09 13:37:23

问题就像我怀疑的那样:内容观察者没有收到有关插入或更新的更新,因为当时应该刷新的列表活动已暂停。要在我的应用程序中插入或更新记录,用户启动不同的活动,在其中编辑记录,然后单击“提交”。

然后在我不知道的情况下执行保存

protected void onActivityResult (int requestCode, int resultCode, Intent data) 

,当我阅读文档时发现 onActivityResult 是:

当您启动的活动退出时调用,为您提供启动它时使用的 requestCode、它返回的 resultCode 以及其中的任何其他数据。如果活动显式返回、未返回任何结果或在操作期间崩溃,则 resultCode 将为 RESULT_CANCELED。

当您的 Activity 重新启动时,您将在 onResume() 之前立即收到此调用。

因此底线是,当我执行插入和更新时,该 Activity 未处于活动状态,因此它没有收到有关以下情况的通知插入或更新。

删除是不同的,因为当从上下文菜单中选择“删除”时,我的应用程序会从列表中删除项目。该活动始终保持活跃状态​​。

The problem was just like I suspected: the content observer was not receiving updates about inserts or updates, because the activity with the list that was supposed to be refreshed was suspended at the time. To insert or update a record in my application, user launches a different activity, where they edit the record, and they click "Submit".

The save is then performed in

protected void onActivityResult (int requestCode, int resultCode, Intent data) 

What I did not know about, and I found out when I read the documentation is that onActivityResult is:

Called when an activity you launched exits, giving you the requestCode you started it with, the resultCode it returned, and any additional data from it. The resultCode will be RESULT_CANCELED if the activity explicitly returned that, didn't return any result, or crashed during its operation.

You will receive this call immediately before onResume() when your activity is re-starting.

So bottom line is that the activity was not active when I performed inserts and updates, and therefore it was not getting notified about inserts or updates.

Deletes are different, because my application deletes items from a list when "Delete" is selected from the context menu. The activity remains active the whole time.

格子衫的從容 2024-11-09 13:37:23

这仍然需要更多的测试,但问题似乎是当光标更新发生时,未收到通知的内容观察者的活动被暂停。并且暂停的活动不会收到事件...我认为...

This still needs some more testing, but it seems that the problem there was that the activity with the content observer that was not getting notified was paused when the cursor updates happened. And a paused activity does not receive events... I think...

违心° 2024-11-09 13:37:23

该调用

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

应该包含对被通知的内容观察者的引用,即

getContext().getContentResolver().notifyChange(notificationUri, MyContentObserver);

The call

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

Should contain a reference to the content observer been notified i.e

getContext().getContentResolver().notifyChange(notificationUri, MyContentObserver);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文