to top
Android APIs
public class

CursorLoader

extends AsyncTaskLoader<D>
java.lang.Object
   ↳ android.content.Loader<D>
     ↳ android.content.AsyncTaskLoader<D>
       ↳ android.content.CursorLoader

Class Overview

A loader that queries the 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[]).

Summary

Public Constructors
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.
Public Methods
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.
void setProjection(String[] projection)
void setSelection(String selection)
void setSelectionArgs(String[] selectionArgs)
void setSortOrder(String sortOrder)
void setUri(Uri uri)
Protected Methods
void onReset()
Subclasses must implement this to take care of resetting their loader, as per reset().
void onStartLoading()
Starts an asynchronous load of the contacts list data.
void onStopLoading()
Must be called from the UI thread
[Expand]
Inherited Methods
From class android.content.AsyncTaskLoader
From class android.content.Loader
From class java.lang.Object

Public Constructors

public CursorLoader (Context context)

Added in API level 11

Creates an empty unspecified CursorLoader. You must follow this with calls to 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)

Added in API level 11

Creates a fully-specified CursorLoader. See ContentResolver.query() for documentation on the meaning of the parameters. These will be passed as-is to that call.

Public Methods

public void cancelLoadInBackground ()

Added in API level 16

Called on the main thread to abort a load in progress. Override this method to abort the current invocation of loadInBackground() that is running in the background on a worker thread. This method should do nothing if loadInBackground() has not started running or if it has already finished.

public void deliverResult (Cursor cursor)

Added in API level 11

Sends the result of the load to the registered listener. Should only be called by subclasses. Must be called from the process's main thread.

Parameters
cursor the result of the load

public void dump (String prefix, FileDescriptor fd, PrintWriter writer, String[] args)

Added in API level 11

Print the Loader's state into the given stream.

Parameters
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.

public String[] getProjection ()

Added in API level 11

public String getSelection ()

Added in API level 11

public String[] getSelectionArgs ()

Added in API level 11

public String getSortOrder ()

Added in API level 11

public Uri getUri ()

Added in API level 11

public Cursor loadInBackground ()

Added in API level 11

Called on a worker thread to perform the actual load and to return the result of the load operation. Implementations should not deliver the result directly, but should return them from this method, which will eventually end up calling deliverResult(D) on the UI thread. If implementations need to process the results on the UI thread they may override deliverResult(D) and do so there. To support cancellation, this method should periodically check the value of isLoadInBackgroundCanceled() and terminate when it returns true. Subclasses may also override cancelLoadInBackground() to interrupt the load directly instead of polling isLoadInBackgroundCanceled(). When the load is canceled, this method may either return normally or throw OperationCanceledException. In either case, the Loader will call onCanceled(D) to perform post-cancellation cleanup and to dispose of the result object, if any.

Returns
  • The result of the load operation.

public void onCanceled (Cursor cursor)

Added in API level 11

Called if the task was canceled before it was completed. Gives the class a chance to clean up post-cancellation and to properly dispose of the result.

Parameters
cursor The value that was returned by loadInBackground(), or null if the task threw OperationCanceledException.

public void setProjection (String[] projection)

Added in API level 11

public void setSelection (String selection)

Added in API level 11

public void setSelectionArgs (String[] selectionArgs)

Added in API level 11

public void setSortOrder (String sortOrder)

Added in API level 11

public void setUri (Uri uri)

Added in API level 11

Protected Methods

protected void onReset ()

Added in API level 11

Subclasses must implement this to take care of resetting their loader, as per reset(). This is not called by clients directly, but as a result of a call to reset(). This will always be called from the process's main thread.

protected void onStartLoading ()

Added in API level 11

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

protected void onStopLoading ()

Added in API level 11

Must be called from the UI thread