android CursorAdapter的监听事件

//contentObserver只是通知数据库中内容变化了

cursor.registerContentObserver(mChangeObserver);

//datasetObserver是调用requery之后通知上层cursor数据内容已经更新
cursor.registerDataSetObserver(mDataSetObserver);

我们的调用流程如下:

当我们使用getContentResolver().query()的时候,我们的resolver会通过uri找到对应的provider,调用相应的query()方法,

该方法中的部分内容:

        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        Cursor ret = qb.query(db, projection, selection,selectionArgs, null, null, finalSortOrder);
        // TODO: Does this need to be a URI for this provider.
        ret.setNotificationUri(getContext().getContentResolver(), uri);
        return ret;

Cursor本身只是接口,实际调用的应该是AbstractCursor

public void setNotificationUri(ContentResolver cr, Uri notifyUri) {
        synchronized (mSelfObserverLock) {
            mNotifyUri = notifyUri;
            mContentResolver = cr;
            if (mSelfObserver != null) {
                mContentResolver.unregisterContentObserver(mSelfObserver);
            }
            mSelfObserver = new SelfContentObserver(this);
            mContentResolver.registerContentObserver(mNotifyUri, true, mSelfObserver);
            mSelfObserverRegistered = true;
        }
    }

//AbstractCursor的内部类

 protected static class SelfContentObserver extends ContentObserver {
        WeakReference<AbstractCursor> mCursor;

        public SelfContentObserver(AbstractCursor cursor) {
            super(null);
            mCursor = new WeakReference<AbstractCursor>(cursor);
        }

        @Override
        public boolean deliverSelfNotifications() {
            return false;
        }

        @Override
        public void onChange(boolean selfChange) {
            AbstractCursor cursor = mCursor.get();
            if (cursor != null) {
                cursor.onChange(false);
            }
        }
    }

ContentObservable mContentObservable = new ContentObservable(); //AbstractCursor持有的contentObservable

//AbstractCursor的onchange()

protected void onChange(boolean selfChange) {
        synchronized (mSelfObserverLock) {
            mContentObservable.dispatchChange(selfChange);
            if (mNotifyUri != null && selfChange) {
                mContentResolver.notifyChange(mNotifyUri, mSelfObserver); //selfChange = false
            }
        }
    }

AbstractCursor中

public void registerContentObserver(ContentObserver observer) {
        mContentObservable.registerObserver(observer);
    }


在ContentObservable中

public void dispatchChange(boolean selfChange) {
        synchronized(mObservers) {
            for (ContentObserver observer : mObservers) {
                if (!selfChange || observer.deliverSelfNotifications()) {
                    observer.dispatchChange(selfChange);
                }
            }
        }
    }

在CursorAdapter中我们注册了ContentObserver

private class ChangeObserver extends ContentObserver {
        public ChangeObserver() {
            super(new Handler());
        }

        @Override
        public boolean deliverSelfNotifications() {
            return true;
        }

        @Override
        public void onChange(boolean selfChange) {
            onContentChanged();
        }
    }

//父类的方法

public final void dispatchChange(boolean selfChange) {  
        if (mHandler == null) {
            onChange(selfChange);
        } else {
            mHandler.post(new NotificationRunnable(selfChange));
        }
    }

//父类的内部类


private final class NotificationRunnable implements Runnable {

        private boolean mSelf;

        public NotificationRunnable(boolean self) {
            mSelf = self;
        }

        public void run() {
            ContentObserver.this.onChange(mSelf);
        }
    }

在CursorAdapter中

protected void onContentChanged() {
        if (mAutoRequery && mCursor != null && !mCursor.isClosed()) {
            if (Config.LOGV) Log.v("Cursor", "Auto requerying " + mCursor + " due to update");
            mDataValid = mCursor.requery();
        }
    }

在AbstractCursor中

public boolean requery() {
        if (mSelfObserver != null && mSelfObserverRegistered == false) {
            mContentResolver.registerContentObserver(mNotifyUri, true, mSelfObserver);
            mSelfObserverRegistered = true;
        }
        mDataSetObservable.notifyChanged(); //任何继承AbstractCursor的Cursor都将通过调用super.requery()执行此句
        return true;
    }

如果我们注册了dataset的observer,就会得到相应的通知。

总结:

contentObserver是一个提前通知,这时候只是通知cursor说,我的内容变化了。

datasetObserver是一个后置通知,只有通过requery() deactivate() close()方法的调用才能获得这个通知。

因此,最为重要的还是ContentObserver,它可以告诉你数据库变化了,当然如果你要在更新完Cursor的dataset之后做一些

事情,datasetObserver也是必需的。


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yangxinle137/archive/2010/11/03/5985515.aspx

你可能感兴趣的:(android)