How to get URI of inserted row in my content observer?

会有一股神秘感。 提交于 2019-12-18 09:03:48

问题


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 .


回答1:


When you insert the new row, you have to manually call notifyChange:

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

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.




回答2:


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 =(



来源:https://stackoverflow.com/questions/8432800/how-to-get-uri-of-inserted-row-in-my-content-observer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!