ContentObserver 获取有关删除的通知,但不获取有关插入或更新的通知
我的活动中有以下代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题就像我怀疑的那样:内容观察者没有收到有关插入或更新的更新,因为当时应该刷新的列表活动已暂停。要在我的应用程序中插入或更新记录,用户启动不同的活动,在其中编辑记录,然后单击“提交”。
然后在我不知道的情况下执行保存
,当我阅读文档时发现 onActivityResult 是:
因此底线是,当我执行插入和更新时,该 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
What I did not know about, and I found out when I read the documentation is that onActivityResult is:
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.
这仍然需要更多的测试,但问题似乎是当光标更新发生时,未收到通知的内容观察者的活动被暂停。并且暂停的活动不会收到事件...我认为...
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...
该调用
应该包含对被通知的内容观察者的引用,即
The call
Should contain a reference to the content observer been notified i.e