首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

Android数据库内容变更的监听

2012-07-02 
Android数据库内容变化的监听onChanged()This method is called when the entire data set has changed, m

Android数据库内容变化的监听

onChanged()This method is called when the entire data set has changed, most likely through a call to?requery()?on a?Cursor.voidonInvalidated()This method is called when the entire data becomes invalid, most likely through a call to?deactivate()?or?close()?on a?Cursor.那么CursorAdapter是如何实现对基于uri的数据库内容变化进行监听呢?
它当然是借助Cursor来实现的。
但是CursorAdapter与ContentObserver相对应对外提供(可以重载)的接口是:
onContentChanged()
当时CursorAdapter与DataSetObserver相对应对外提供(可以重载)的接口是:
onChanged()->notifyDataSetChanged()//这个消息并完全不是从Cursor传递来,它会在CursorAdapter内手动被调用
onInvalidated()->notifyDataSetInvalidated()//这个消息并不是从Cursor传递来。
具体可以参照附件2的android.widget.CursorAdapter源代码
CursorAdapter的notifyDataSetChanged()和notifyDataSetInvalidated()事件可以继续往外传递 BaseAdapter为此提供了以下系列方法。
voidnotifyDataSetChanged()Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.voidnotifyDataSetInvalidated()Notifies the attached observers that the underlying data is no longer valid or available.voidregisterDataSetObserver(DataSetObserver?observer)Register an observer that is called when changes happen to the data used by this adapter.voidunregisterDataSetObserver(DataSetObserver?observer)Unregister an observer that has previously been registered with this adapter via?registerDataSetObserver(DataSetObserver).android.widget.BaseAdapter的部分源代码如下:
public abstract class BaseAdapter implements ListAdapter, SpinnerAdapter {?private final DataSetObservable mDataSetObservable = new DataSetObservable();?public boolean hasStableIds() { return false; }?public void registerDataSetObserver(DataSetObserver observer) { mDataSetObservable.registerObserver(observer); }?public void unregisterDataSetObserver(DataSetObserver observer) { mDataSetObservable.unregisterObserver(observer); }?/** * Notifies the attached View that the underlying data has been changed * and it should refresh itself. */?public void notifyDataSetChanged() { mDataSetObservable.notifyChanged(); }?public void notifyDataSetInvalidated() { mDataSetObservable.notifyInvalidated(); }?-------------------省略------------------}?注意:对于BaseAdapter,外部可以强行调用notifyDataSetChanged和notifyDataSetInvalidated来通知DataSetObserver observer

?

转:

http://hubingforever.blog.163.com/blog/static/1710405792010101541326636/

?

热点排行