完成后 swapCursor 在适配器中调用 newView

does swapCursor call newView in the adapter after it is finished

在 onLoadFinished() 中我使用 myadapter.swapCuesor(cursor) 并且我想知道它之后是否在适配器中调用 newView?它没有写在 android 的适配器的 api 中,所以我在这里问。如果不是,那么适配器如何自我更新?

当您使用 CursorLoader 时,Cursor 是为您管理的。您唯一需要做的就是实现以下三个方法:

    // Called when a new Loader needs to be created
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    // Now create and return a CursorLoader that will take care of
    // creating a Cursor for the data being displayed.
    return new CursorLoader(this, ContactsContract.Data.CONTENT_URI,
        PROJECTION, SELECTION, null, null);
   }

   // Called when a previously created loader has finished loading
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    // Swap the new cursor in.  (The framework will take care of closing the
    // old cursor once we return.)
    mAdapter.swapCursor(data);
    }

   // Called when a previously created loader is reset, making the data            unavailable
public void onLoaderReset(Loader<Cursor> loader) {
// This is called when the last Cursor provided to onLoadFinished()
// above is about to be closed.  We need to make sure we are no
// longer using it.
mAdapter.swapCursor(null);
} 

您不必自己打开和关闭 Cursor,加载程序会为您完成此操作。这是你必须使用 swapCursor 的最重要原因,当你将它与另一个 Cursor 交换时它不会关闭 Cursor。

    public Cursor swapCursor(Cursor newCursor) {
    if (newCursor == mCursor) {
        return null;
    }
    Cursor oldCursor = mCursor;
    if (oldCursor != null) {
        if (mChangeObserver != null) oldCursor.unregisterContentObserver(mChangeObserver);
        if (mDataSetObserver != null) oldCursor.unregisterDataSetObserver(mDataSetObserver);
    }
    mCursor = newCursor;
    if (newCursor != null) {
        if (mChangeObserver != null) newCursor.registerContentObserver(mChangeObserver);
        if (mDataSetObserver != null) newCursor.registerDataSetObserver(mDataSetObserver);
        mRowIDColumn = newCursor.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();
    }
    return oldCursor;
}

ChangeCursor 另一方面,首先将当前 Cursor 与新 Cursor 交换,然后为您关闭它。如果您将此方法与您的 CursorLoader 一起使用,您的应用有时可能会崩溃。

    public void changeCursor(Cursor cursor) {
    Cursor old = swapCursor(cursor);
    if (old != null) {
        old.close();
      }
    }