CursorLoader


public class CursorLoader
extends AsyncTaskLoader<Cursor>



This class was deprecated in API level 28.
Use the Support Library CursorLoader

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(android.content.Context, android.net.Uri, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String) or creating an empty instance with CursorLoader(android.content.Context) and filling in the desired parameters with setUri(android.net.Uri), setSelection(java.lang.String), setSelectionArgs(java.lang.String[]), setSortOrder(java.lang.String), and setProjection(java.lang.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 data.

void onStopLoading()

Must be called from the UI thread

Inherited methods

void cancelLoadInBackground()

Called on the main thread to abort a load in progress.

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

Print the Loader's state into the given stream.

boolean isLoadInBackgroundCanceled()

Returns true if the current invocation of loadInBackground() is being canceled.

abstract Cursor loadInBackground()

Called on a worker thread to perform the actual load and to return the result of the load operation.

boolean onCancelLoad()

Subclasses must implement this to take care of requests to cancelLoad().

void onCanceled(Cursor data)

Called if the task was canceled before it was completed.

void onForceLoad()

Subclasses must implement this to take care of requests to forceLoad().

Cursor onLoadInBackground()

Calls loadInBackground().

void setUpdateThrottle(long delayMS)

Set amount to throttle updates by.

void abandon()

This function will normally be called for you automatically by LoaderManager when restarting a Loader.

boolean cancelLoad()

Attempt to cancel the current load task.

void commitContentChanged()

Commit that you have actually fully processed a content change that was returned by takeContentChanged().

String dataToString(Cursor data)

For debugging, converts an instance of the Loader's data class to a string that can be printed.

void deliverCancellation()

Informs the registered OnLoadCanceledListener that the load has been canceled.

void deliverResult(Cursor data)

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.

void forceLoad()

Force an asynchronous load.

Context getContext()
int getId()
boolean isAbandoned()

Return whether this loader has been abandoned.

boolean isReset()

Return whether this load has been reset.

boolean isStarted()

Return whether this load has been started.

void onAbandon()

Subclasses implement this to take care of being abandoned.

boolean onCancelLoad()

Subclasses must implement this to take care of requests to cancelLoad().

void onContentChanged()

Called when ForceLoadContentObserver detects a change.

void onForceLoad()

Subclasses must implement this to take care of requests to forceLoad().

void onReset()

Subclasses must implement this to take care of resetting their loader, as per reset().

void onStartLoading()

Subclasses must implement this to take care of loading their data, as per startLoading().

void onStopLoading()

Subclasses must implement this to take care of stopping their loader, as per stopLoading().

void registerListener(int id, OnLoadCompleteListener<Cursor> listener)

Registers a class that will receive callbacks when a load is complete.

void registerOnLoadCanceledListener(OnLoadCanceledListener<Cursor> listener)

Registers a listener that will receive callbacks when a load is canceled.

void reset()

This function will normally be called for you automatically by LoaderManager when destroying a Loader.

void rollbackContentChanged()

Report that you have abandoned the processing of a content change that was returned by takeContentChanged() and would like to rollback to the state where there is again a pending content change.

final void startLoading()

This function will normally be called for you automatically by LoaderManager when the associated fragment/activity is being started.

void stopLoading()

This function will normally be called for you automatically by LoaderManager when the associated fragment/activity is being stopped.

boolean takeContentChanged()

Take the current flag indicating whether the loader's content had changed while it was stopped.

String toString()

Returns a string representation of the object.

void unregisterListener(OnLoadCompleteListener<Cursor> listener)

Remove a listener that was previously added with registerListener(int, OnLoadCompleteListener).

void unregisterOnLoadCanceledListener(OnLoadCanceledListener<Cursor> listener)

Unregisters a listener that was previously added with registerOnLoadCanceledListener(OnLoadCanceledListener).

Object clone()

Creates and returns a copy of this object.

boolean equals(Object obj)

Indicates whether some other object is "equal to" this one.

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

final Class<?> getClass()

Returns the runtime class of this Object.

int hashCode()

Returns a hash code value for the object.

final void notify()

Wakes up a single thread that is waiting on this object's monitor.

final void notifyAll()

Wakes up all threads that are waiting on this object's monitor.

String toString()

Returns a string representation of the object.

final void wait(long timeoutMillis, int nanos)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait(long timeoutMillis)

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

final void wait()

Causes the current thread to wait until it is awakened, typically by being notified or interrupted.

Public constructors

CursorLoader

Added in API level 11
public CursorLoader (Context context)

Creates an empty unspecified CursorLoader. You must follow this with calls to setUri(android.net.Uri), setSelection(java.lang.String), etc to specify the query to perform.

Parameters
context Context

CursorLoader

Added in API level 11
public CursorLoader (Context context, 
                Uri uri, 
                String[] projection, 
                String selection, 
                String[] selectionArgs, 
                String sortOrder)

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.

Parameters
context Context

uri Uri

projection String

selection String

selectionArgs String

sortOrder String

Public methods

cancelLoadInBackground

Added in API level 16
Deprecated in API level 28
public void cancelLoadInBackground ()

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.

deliverResult

Added in API level 11
public void deliverResult (Cursor cursor)

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 Cursor: the result of the load

dump

Added in API level 11
public void dump (String prefix, 
                FileDescriptor fd, 
                PrintWriter writer, 
                String[] args)

Print the Loader's state into the given stream.

Parameters
prefix String: Text to print at the front of each line.

fd FileDescriptor: The raw file descriptor that the dump is being sent to.

writer PrintWriter: A PrintWriter to which the dump is to be set.

args String: Additional arguments to the dump request.

getProjection

Added in API level 11
public String[] getProjection ()

Returns
String[]

getSelection

Added in API level 11
public String getSelection ()

Returns
String

getSelectionArgs

Added in API level 11
public String[] getSelectionArgs ()

Returns
String[]

getSortOrder

Added in API level 11
public String getSortOrder ()

Returns
String

getUri

Added in API level 11
public Uri getUri ()

Returns
Uri

loadInBackground

Added in API level 11
public Cursor loadInBackground ()

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 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 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
Cursor The result of the load operation.

onCanceled

Added in API level 11
public void onCanceled (Cursor cursor)

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 Cursor: The value that was returned by loadInBackground(), or null if the task threw OperationCanceledException.

setProjection

Added in API level 11
public void setProjection (String[] projection)

Parameters
projection String

setSelection

Added in API level 11
public void setSelection (String selection)

Parameters
selection String

setSelectionArgs

Added in API level 11
public void setSelectionArgs (String[] selectionArgs)

Parameters
selectionArgs String

setSortOrder

Added in API level 11
public void setSortOrder (String sortOrder)

Parameters
sortOrder String

setUri

Added in API level 11
public void setUri (Uri uri)

Parameters
uri Uri

Protected methods

onReset

Added in API level 11
protected void onReset ()

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.

onStartLoading

Added in API level 11
protected void onStartLoading ()

Starts an asynchronous load of the 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

onStopLoading

Added in API level 11
protected void onStopLoading ()

Must be called from the UI thread