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 .
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.
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