如何获取内容观察器中插入行的 URI?
在我的应用程序中,我在数据库中插入来自不同类的值,这给了我插入行的 ID。 我已经在我的 UI 中注册了它的观察者,每当插入任何行时,我的 UI 内容观察者都会被调用。 这两个类完全不同,所以我发现很难获得插入的行 ID。 有什么方法可以在我的内容观察器中获取此行 ID,以便我可以相应地更新我的 UI?
PS context.getContentResolver().notifyChange(uri, null);不是我的解决方案,因为我希望在 UI 上生效(更新受影响的更改)。
In my application, I am inserting values in database from a different class which gives me id of inserted row.
I have registered an observer of it in my UI and whenever any row is inserted, my UI content observer will get called.
Both the classes are entirely different so I am finding it difficult to get inserted row id.
Is there any way to get this row id in my content observer so that I can update my UI accordingly?
P.S. context.getContentResolver().notifyChange(uri, null); is not my solution, as i want effected in ( to update effected change ) on UI .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
插入新行时,必须手动调用 notifyChange:
插入行的代码应该能够访问新的 rowid,并且应该能够构造 URI 以传递给 notifyChange。当调用通知更改时,它应该会导致所有
ContentObservers
被更新。When you insert the new row, you have to manually call notifyChange:
The code that inserted the row should have access to the new rowid, and should be able to construct the URI to pass to notifyChange. When notify change is called, it should cause all
ContentObservers
to be updated.现在,在 android api 16 中,可以通过将 URI 作为参数的 onChange 版本在 OnChage 方法中获取已修改行的 URI。您可以在 Content Observer 文档中找到更多详细信息
http://developer.android.com/reference/android/database/ContentObserver。 对于较旧的 api版本
,我不确定是否有解决方案(无需添加更多逻辑,例如在数据库上实现标志)。我问了关于“ 在旧版本的 API 上更改 ContentObserver Onchange 上的 uri ”并且没有得到任何答案 =(
Now in android api 16 there is the possibility of getting the URI of the modified row in the OnChage method through a version of the onChange which takes the URI as a parameter. You can find more details about it on the Content Observer documentation
http://developer.android.com/reference/android/database/ContentObserver.html
For on older api versions I am not sure if there is solution (without adding more logic such as implementing flags on your database). I asked that question regarding specifically the old api on " getting changed uri on ContentObserver Onchange on older versions of the API " and did not get any answer =(