FileDocCategorySizeDatePackage
CursorLoader.javaAPI DocAndroid 5.1 API7566Thu Mar 12 22:22:10 GMT 2015android.content

CursorLoader

public class CursorLoader extends AsyncTaskLoader
A loader that queries the {@link ContentResolver} and returns a {@link Cursor}. This class implements the {@link Loader} protocol in a standard way for querying cursors, building on {@link AsyncTaskLoader} to perform the cursor query on a background thread so that it does not block the application's UI.

A CursorLoader must be built with the full information for the query to perform, either through the {@link #CursorLoader(Context, Uri, String[], String, String[], String)} or creating an empty instance with {@link #CursorLoader(Context)} and filling in the desired paramters with {@link #setUri(Uri)}, {@link #setSelection(String)}, {@link #setSelectionArgs(String[])}, {@link #setSortOrder(String)}, and {@link #setProjection(String[])}.

Fields Summary
final ForceLoadContentObserver
mObserver
android.net.Uri
mUri
String[]
mProjection
String
mSelection
String[]
mSelectionArgs
String
mSortOrder
android.database.Cursor
mCursor
android.os.CancellationSignal
mCancellationSignal
Constructors Summary
public CursorLoader(Context context)
Creates an empty unspecified CursorLoader. You must follow this with calls to {@link #setUri(Uri)}, {@link #setSelection(String)}, etc to specify the query to perform.

        super(context);
        mObserver = new ForceLoadContentObserver();
    
public CursorLoader(Context context, android.net.Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
Creates a fully-specified CursorLoader. See {@link ContentResolver#query(Uri, String[], String, String[], String) ContentResolver.query()} for documentation on the meaning of the parameters. These will be passed as-is to that call.

        super(context);
        mObserver = new ForceLoadContentObserver();
        mUri = uri;
        mProjection = projection;
        mSelection = selection;
        mSelectionArgs = selectionArgs;
        mSortOrder = sortOrder;
    
Methods Summary
public voidcancelLoadInBackground()

        super.cancelLoadInBackground();

        synchronized (this) {
            if (mCancellationSignal != null) {
                mCancellationSignal.cancel();
            }
        }
    
public voiddeliverResult(android.database.Cursor cursor)

        if (isReset()) {
            // An async query came in while the loader is stopped
            if (cursor != null) {
                cursor.close();
            }
            return;
        }
        Cursor oldCursor = mCursor;
        mCursor = cursor;

        if (isStarted()) {
            super.deliverResult(cursor);
        }

        if (oldCursor != null && oldCursor != cursor && !oldCursor.isClosed()) {
            oldCursor.close();
        }
    
public voiddump(java.lang.String prefix, java.io.FileDescriptor fd, java.io.PrintWriter writer, java.lang.String[] args)

        super.dump(prefix, fd, writer, args);
        writer.print(prefix); writer.print("mUri="); writer.println(mUri);
        writer.print(prefix); writer.print("mProjection=");
                writer.println(Arrays.toString(mProjection));
        writer.print(prefix); writer.print("mSelection="); writer.println(mSelection);
        writer.print(prefix); writer.print("mSelectionArgs=");
                writer.println(Arrays.toString(mSelectionArgs));
        writer.print(prefix); writer.print("mSortOrder="); writer.println(mSortOrder);
        writer.print(prefix); writer.print("mCursor="); writer.println(mCursor);
        writer.print(prefix); writer.print("mContentChanged="); writer.println(mContentChanged);
    
public java.lang.String[]getProjection()

        return mProjection;
    
public java.lang.StringgetSelection()

        return mSelection;
    
public java.lang.String[]getSelectionArgs()

        return mSelectionArgs;
    
public java.lang.StringgetSortOrder()

        return mSortOrder;
    
public android.net.UrigetUri()

        return mUri;
    
public android.database.CursorloadInBackground()

        synchronized (this) {
            if (isLoadInBackgroundCanceled()) {
                throw new OperationCanceledException();
            }
            mCancellationSignal = new CancellationSignal();
        }
        try {
            Cursor cursor = getContext().getContentResolver().query(mUri, mProjection, mSelection,
                    mSelectionArgs, mSortOrder, mCancellationSignal);
            if (cursor != null) {
                try {
                    // Ensure the cursor window is filled.
                    cursor.getCount();
                    cursor.registerContentObserver(mObserver);
                } catch (RuntimeException ex) {
                    cursor.close();
                    throw ex;
                }
            }
            return cursor;
        } finally {
            synchronized (this) {
                mCancellationSignal = null;
            }
        }
    
public voidonCanceled(android.database.Cursor cursor)

        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
    
protected voidonReset()

        super.onReset();
        
        // Ensure the loader is stopped
        onStopLoading();

        if (mCursor != null && !mCursor.isClosed()) {
            mCursor.close();
        }
        mCursor = null;
    
protected voidonStartLoading()
Starts an asynchronous load of the contacts list data. When the result is ready the callbacks will be called on the UI thread. If a previous load has been completed and is still valid the result may be passed to the callbacks immediately. Must be called from the UI thread

        if (mCursor != null) {
            deliverResult(mCursor);
        }
        if (takeContentChanged() || mCursor == null) {
            forceLoad();
        }
    
protected voidonStopLoading()
Must be called from the UI thread

        // Attempt to cancel the current load task if possible.
        cancelLoad();
    
public voidsetProjection(java.lang.String[] projection)

        mProjection = projection;
    
public voidsetSelection(java.lang.String selection)

        mSelection = selection;
    
public voidsetSelectionArgs(java.lang.String[] selectionArgs)

        mSelectionArgs = selectionArgs;
    
public voidsetSortOrder(java.lang.String sortOrder)

        mSortOrder = sortOrder;
    
public voidsetUri(android.net.Uri uri)

        mUri = uri;