我从
this tutorial开始研究如何使用ContentProviders和Loaders
我怎么看
我们有一个ListView,SimpleCursorAdapter和CursorLoader的Activity.我们还实现了ContentProvider.
在一个Activity中,我们可以调用getContentResolver().insert(URI,contentValues);通过按钮点击.
在我们实现ContentProvider的最后的insert()方法中,我们调用getContentResolver().notifyChange(URI,null);我们的CursorLoader将收到消息,它应该重新加载数据并更新UI.另外,如果我们在SimpleCursorAdapter中使用FLAG_REGISTER_CONTENT_OBSERVER,它也会收到消息,并且将调用其onContentChanged()方法.
如果我们插入,更新或删除数据,我们的ListView将被更新.
Activity.startManagingCursor(光标);被弃用,cursor.requery()被弃用,所以我没有看到任何从cursor.setNotificationUri()的实践感.
我查看了setNotificationUri()方法的源代码,并看到它在方法内调用了mContentResolver.registerContentObserver(mNotifyUri,true,mSelfObserver). CursorLoader也是一样的.最后光标将接收消息,并在Cursor中调用以下方法:
protected void onChange(boolean selfChange) {
synchronized (mSelfObserverLock) {
mContentObservable.dispatchChange(selfChange, null);
// ...
}
}
但我不明白这一点.
所以我的问题是:为什么我们在ContentProvider实现的query()方法中调用cursor.setNotificationUri()?