public class CursorLoader extends AsyncTaskLoader<Cursor>
ContentResolver
and returns a Cursor
.
This class implements the Loader
protocol in a standard way for
querying cursors, building on 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
CursorLoader(Context, Uri, String[], String, String[], String)
or
creating an empty instance with CursorLoader(Context)
and filling
in the desired paramters with setUri(Uri)
, setSelection(String)
,
setSelectionArgs(String[])
, setSortOrder(String)
,
and setProjection(String[])
.
Loader.ForceLoadContentObserver, Loader.OnLoadCanceledListener<D>, Loader.OnLoadCompleteListener<D>
Constructor and Description |
---|
CursorLoader(Context context)
Creates an empty unspecified CursorLoader.
|
CursorLoader(Context context,
Uri uri,
String[] projection,
String selection,
String[] selectionArgs,
String sortOrder)
Creates a fully-specified CursorLoader.
|
Modifier and Type | Method and Description |
---|---|
void |
cancelLoadInBackground()
Called on the main thread to abort a load in progress.
|
void |
deliverResult(Cursor cursor)
Sends the result of the load to the registered listener.
|
void |
dump(String prefix,
FileDescriptor fd,
PrintWriter writer,
String[] args)
Print the Loader's state into the given stream.
|
String[] |
getProjection() |
String |
getSelection() |
String[] |
getSelectionArgs() |
String |
getSortOrder() |
Uri |
getUri() |
Cursor |
loadInBackground()
Called on a worker thread to perform the actual load and to return
the result of the load operation.
|
void |
onCanceled(Cursor cursor)
Called if the task was canceled before it was completed.
|
protected void |
onReset()
Subclasses must implement this to take care of resetting their loader,
as per
Loader.reset() . |
protected void |
onStartLoading()
Starts an asynchronous load of the contacts list data.
|
protected void |
onStopLoading()
Must be called from the UI thread
|
void |
setProjection(String[] projection) |
void |
setSelection(String selection) |
void |
setSelectionArgs(String[] selectionArgs) |
void |
setSortOrder(String sortOrder) |
void |
setUri(Uri uri) |
isLoadInBackgroundCanceled, onCancelLoad, onForceLoad, onLoadInBackground, setUpdateThrottle, waitForLoader
abandon, cancelLoad, dataToString, deliverCancellation, forceLoad, getContext, getId, isAbandoned, isReset, isStarted, onAbandon, onContentChanged, registerListener, registerOnLoadCanceledListener, reset, startLoading, stopLoading, takeContentChanged, toString, unregisterListener, unregisterOnLoadCanceledListener
public CursorLoader(Context context)
setUri(Uri)
, setSelection(String)
, etc
to specify the query to perform.public CursorLoader(Context context, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
ContentResolver.query()
for documentation on the meaning of the
parameters. These will be passed as-is to that call.public Cursor loadInBackground()
AsyncTaskLoader
Loader.deliverResult(D)
on
the UI thread. If implementations need to process the results on the UI thread
they may override Loader.deliverResult(D)
and do so there.
To support cancellation, this method should periodically check the value of
AsyncTaskLoader.isLoadInBackgroundCanceled()
and terminate when it returns true.
Subclasses may also override AsyncTaskLoader.cancelLoadInBackground()
to interrupt the load
directly instead of polling AsyncTaskLoader.isLoadInBackgroundCanceled()
.
When the load is canceled, this method may either return normally or throw
OperationCanceledException
. In either case, the Loader
will
call AsyncTaskLoader.onCanceled(D)
to perform post-cancellation cleanup and to dispose of the
result object, if any.loadInBackground
in class AsyncTaskLoader<Cursor>
AsyncTaskLoader.isLoadInBackgroundCanceled()
,
AsyncTaskLoader.cancelLoadInBackground()
,
AsyncTaskLoader.onCanceled(D)
public void cancelLoadInBackground()
AsyncTaskLoader
AsyncTaskLoader.loadInBackground()
that is running in the background on a worker thread.
This method should do nothing if AsyncTaskLoader.loadInBackground()
has not started
running or if it has already finished.cancelLoadInBackground
in class AsyncTaskLoader<Cursor>
AsyncTaskLoader.loadInBackground()
public void deliverResult(Cursor cursor)
Loader
deliverResult
in class Loader<Cursor>
cursor
- the result of the loadprotected void onStartLoading()
onStartLoading
in class Loader<Cursor>
protected void onStopLoading()
onStopLoading
in class Loader<Cursor>
public void onCanceled(Cursor cursor)
AsyncTaskLoader
onCanceled
in class AsyncTaskLoader<Cursor>
cursor
- The value that was returned by AsyncTaskLoader.loadInBackground()
, or null
if the task threw OperationCanceledException
.protected void onReset()
Loader
Loader.reset()
. This is not called by clients directly,
but as a result of a call to Loader.reset()
.
This will always be called from the process's main thread.public Uri getUri()
public void setUri(Uri uri)
public String[] getProjection()
public void setProjection(String[] projection)
public String getSelection()
public void setSelection(String selection)
public String[] getSelectionArgs()
public void setSelectionArgs(String[] selectionArgs)
public String getSortOrder()
public void setSortOrder(String sortOrder)
public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args)
Loader
dump
in class AsyncTaskLoader<Cursor>
prefix
- Text to print at the front of each line.fd
- The raw file descriptor that the dump is being sent to.writer
- A PrintWriter to which the dump is to be set.args
- Additional arguments to the dump request.