Android對資料庫表的一個約定:每張表都應該至少有_id這列

來源:互聯網
上載者:User

        Android對資料庫表有一個約定。就是每張表都應該至少有_id這列。ListView在使用CursorAdapter及其子類適配 cursor的時候,會預設的擷取 _id 這列的值,如果你建的表沒有 _id這列或者你的cursor中沒有_id這列(查詢時的projection中沒有_id)就報錯了。所以使用CursorAdapter及其子類的時候一定要使查詢時的projection包含_id。CursorAdapter中相關代碼如下:

1.注釋/** * Adapter that exposes data from a {@link android.database.Cursor Cursor} to a  * {@link android.widget.ListView ListView} widget. The Cursor must include  * a column named "_id" or this class will not work. */2.構造方法    public CursorAdapter(Context context, Cursor c) {        init(context, c, true);    }public CursorAdapter(Context context, Cursor c, boolean autoRequery) {        init(context, c, autoRequery);    }    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);        }    }3.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();        }    }

其中cursor.getColumnIndexOrThrow(String columnName)如下:

/**     * Returns the zero-based index for the given column name, or throws     * {@link IllegalArgumentException} if the column doesn't exist. If you're not sure if     * a column will exist or not use {@link #getColumnIndex(String)} and check for -1, which     * is more efficient than catching the exceptions.     *     * @param columnName the name of the target column.     * @return the zero-based column index for the given column name     * @see #getColumnIndex(String)     * @throws IllegalArgumentException if the column does not exist     */    int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException;



     所以只要Cursor不為空白,Cursor中必須存在_id列,CursorAdapter及其子類才能正常工作。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.