Android資料庫內容變化的監聽(附件2)

來源:互聯網
上載者:User

public abstract class CursorAdapter extends BaseAdapter implements Filterable,

        CursorFilter.CursorFilterClient {
-------------------省略------------------

    protected ChangeObserver mChangeObserver;
    /**

     * This field should be made private, so it is hidden from the SDK.

     * {@hide}

     */

    protected DataSetObserver mDataSetObserver = new MyDataSetObserver();
    /**

     * This field should be made private, so it is hidden from the SDK.

     * {@hide}

     */
-------------------省略------------------

    public CursorAdapter(Context context, Cursor c) {

        init(context, c, true);

    }
-------------------省略------------------

    protected void init(Context context, Cursor c, boolean autoRequery) {

        boolean cursorPresent = c != null;

        mAutoRequery = autoRequery;

        mCursor = c;

        mDataValid = cursorPresent;

        mContext = context;

        mRowIDColumn = cursorPresent ? c.getColumnIndexOrThrow("_id") : -1;

        mChangeObserver = new ChangeObserver();

        if (cursorPresent) {
            c.registerContentObserver(mChangeObserver);

            c.registerDataSetObserver(mDataSetObserver);

        }

    }
-------------------省略------------------    

    /**

     * Change the underlying cursor to a new cursor. If there is an existing cursor it will be

     * closed.

     * 

     * @param cursor the new cursor to be used

     */

    public void changeCursor(Cursor cursor) {

        if (cursor == mCursor) {

            return;

        }

        if (mCursor != null) {
            mCursor.unregisterContentObserver(mChangeObserver);

            mCursor.unregisterDataSetObserver(mDataSetObserver);

            mCursor.close();

        }

        mCursor = cursor;

        if (cursor != null) {
            cursor.registerContentObserver(mChangeObserver);

            cursor.registerDataSetObserver(mDataSetObserver);

            mRowIDColumn = cursor.getColumnIndexOrThrow("_id");

            mDataValid = true;

            // notify the observers about the new cursor

            notifyDataSetChanged();

        } else {

            mRowIDColumn = -1;

            mDataValid = false;

            // notify the observers about the lack of a data set

            notifyDataSetInvalidated();

        }

    }
-------------------省略------------------
    /**

     * Called when the {@link ContentObserver} on the cursor receives a change notification.

     * The default implementation provides the auto-requery logic, but may be overridden by

     * sub classes.

     * 

     * @see ContentObserver#onChange(boolean)

     */

    protected void onContentChanged() {

        if (mAutoRequery && mCursor != null && !mCursor.isClosed()) {

            if (Config.LOGV) Log.v("Cursor", "Auto requerying " + mCursor + " due to update");

            mDataValid = mCursor.requery();

        }

    }

    private class ChangeObserver extends ContentObserver {

        public ChangeObserver() {

            super(new Handler());

        }


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

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

    private class MyDataSetObserver extends DataSetObserver {
        @Override
        public void onChanged() {
            mDataValid = true;
            notifyDataSetChanged();
        }
        @Override
        public void onInvalidated() {
            mDataValid = false;
            notifyDataSetInvalidated();
        }
    }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.