首页 > 学院 > 开发设计 > 正文

CursorAdapter监听数据变化通知界面刷新

2019-11-06 09:35:03
字体:
来源:转载
供稿:网友

ContentObserver 监听数据库变化

AbstractCursor.setNotificationUri(ContentResolver cr, Uri notifyUri) -> ContentResolver.registerContentObserver(Uri uri, boolean notifyForDescendents, ContentObserver observer, int userHandle)AbstractCursor.registerContentObserver(ContentObserver observer)SelfContentObserver.onChange(boolean selfChange) -> AbstractCursor.(boolean selfChange)

DataSetObserver 监听cursor close/deactivate/requery

AbstractCursor.registerDataSetObserver(DataSetObserver observer)AbstractCursor.close() / deactivate() -> AbstractCursor.onDeactivateOrClose() -> DataSetObservable.notifyInvalidated()AbstractCursor.requery() -> DataSetObservable.notifyChanged()

notifyDataSetChanged 与 notifyDataSetInvalidated 区别 (@AdapterView.java): 前者会保留listview的部分状态,而后者会重置listview的状态

class AdapterDataSetObserver extends DataSetObserver { PRivate Parcelable mInstanceState = null; @Override public void onChanged() { mDataChanged = true; mOldItemCount = mItemCount; mItemCount = getAdapter().getCount(); // Detect the case where a cursor that was previously invalidated has // been repopulated with new data. if (AdapterView.this.getAdapter().hasStableIds() && mInstanceState != null && mOldItemCount == 0 && mItemCount > 0) { AdapterView.this.onRestoreInstanceState(mInstanceState); mInstanceState = null; } else { rememberSyncState(); } checkFocus(); requestLayout(); } @Override public void onInvalidated() { mDataChanged = true; if (AdapterView.this.getAdapter().hasStableIds()) { // Remember the current state for the case where our hosting activity is being // stopped and later restarted mInstanceState = AdapterView.this.onSaveInstanceState(); } // Data is invalid so we should reset our state mOldItemCount = mItemCount; mItemCount = 0; mSelectedPosition = INVALID_POSITION; mSelectedRowId = INVALID_ROW_ID; mNextSelectedPosition = INVALID_POSITION; mNextSelectedRowId = INVALID_ROW_ID; mNeedSync = false; checkFocus(); requestLayout(); } public void clearSavedState() { mInstanceState = null; } }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表