1. contentobserver
Contentobserver monitors specific databases tables through Uris. If the databases table changes, the data in the updated cursor is notified.
If contentprovider is used to operate the database, the query () method of contentprovider uses cursor. seticationicationuri () registers the table described by Uri. getcontext () is called after the insert, delete, and query operations (). getcontentresolver (). notifychange (). After the insert, delete, and query operations are performed on the DB table described by the URI, notifychange () notifies the contentobserver registered by the cursor and calls the onchange method of contentobserver. Generally, the onchange of cursoradapter calls oncontentchanged and calls cursor. requery () in oncontentchanged to update the data in cursor.
Purpose: Notify the user to refresh the data in cursor after there is a change in the database table.
2. datasetobserver
Datasetobserver is mainly used to call the methods when the cursor for registration changes, so that users can perform interface refresh and other operations.
First, the cursor registers the datasetobserver through registerdatasetobserver (). When the cursor data changes, for example, when the cursor requery () is called, The onchanged cursor is called to notify the user that the content in the cursor has changed, you can refresh the interface in onchanged. Generally, the notifydatasetchanged notification framework is called in onchanged. When the framework receives a notification, the getview of cursoradapter is called to refresh the interface.
Purpose: Notify the user to refresh the interface when the data in the cursor changes.