CursorLoaderpublic 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 void | cancelLoadInBackground()
super.cancelLoadInBackground();
synchronized (this) {
if (mCancellationSignal != null) {
mCancellationSignal.cancel();
}
}
| public void | deliverResult(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 void | dump(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.String | getSelection()
return mSelection;
| public java.lang.String[] | getSelectionArgs()
return mSelectionArgs;
| public java.lang.String | getSortOrder()
return mSortOrder;
| public android.net.Uri | getUri()
return mUri;
| public android.database.Cursor | loadInBackground()
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 void | onCanceled(android.database.Cursor cursor)
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
| protected void | onReset()
super.onReset();
// Ensure the loader is stopped
onStopLoading();
if (mCursor != null && !mCursor.isClosed()) {
mCursor.close();
}
mCursor = null;
| protected void | onStartLoading()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 void | onStopLoading()Must be called from the UI thread
// Attempt to cancel the current load task if possible.
cancelLoad();
| public void | setProjection(java.lang.String[] projection)
mProjection = projection;
| public void | setSelection(java.lang.String selection)
mSelection = selection;
| public void | setSelectionArgs(java.lang.String[] selectionArgs)
mSelectionArgs = selectionArgs;
| public void | setSortOrder(java.lang.String sortOrder)
mSortOrder = sortOrder;
| public void | setUri(android.net.Uri uri)
mUri = uri;
|
|