Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)
Android APIs
public abstract class

CursorAdapter

extends BaseAdapter
implements Filterable
java.lang.Object
   ↳ android.widget.BaseAdapter
     ↳ android.widget.CursorAdapter
Known Direct Subclasses
Known Indirect Subclasses

Class Overview

Adapter that exposes data from a Cursor to a ListView widget.

The Cursor must include a column named "_id" or this class will not work. Additionally, using MergeCursor with this class will not work if the merged Cursors have overlapping values in their "_id" columns.

Summary

Constants
int FLAG_AUTO_REQUERY This constant was deprecated in API level 11. This option is discouraged, as it results in Cursor queries being performed on the application's UI thread and thus can cause poor responsiveness or even Application Not Responding errors. As an alternative, use LoaderManager with a CursorLoader .
int FLAG_REGISTER_CONTENT_OBSERVER If set the adapter will register a content observer on the cursor and will call onContentChanged() when a notification comes in.
[Expand]
Inherited Constants
From interface android.widget.Adapter
Public Constructors
CursorAdapter ( Context context, Cursor c)
This constructor was deprecated in API level 11. This option is discouraged, as it results in Cursor queries being performed on the application's UI thread and thus can cause poor responsiveness or even Application Not Responding errors. As an alternative, use LoaderManager with a CursorLoader .
CursorAdapter ( Context context, Cursor c, boolean autoRequery)
Constructor that allows control over auto-requery.
CursorAdapter ( Context context, Cursor c, int flags)
Recommended constructor.
Public Methods
abstract void bindView ( View view, Context context, Cursor cursor)
Bind an existing view to the data pointed to by cursor
void changeCursor ( Cursor cursor)
Change the underlying cursor to a new cursor.
CharSequence convertToString ( Cursor cursor)

Converts the cursor into a CharSequence.

int getCount ()
Cursor getCursor ()
Returns the cursor.
View getDropDownView (int position, View convertView, ViewGroup parent)

Get a View that displays in the drop down popup the data at the specified position in the data set.

Filter getFilter ()

Returns a filter that can be used to constrain data with a filtering pattern.

FilterQueryProvider getFilterQueryProvider ()
Returns the query filter provider used for filtering.
Object getItem (int position)
long getItemId (int position)
View getView (int position, View convertView, ViewGroup parent)
boolean hasStableIds ()
Indicates whether the item ids are stable across changes to the underlying data.
View newDropDownView ( Context context, Cursor cursor, ViewGroup parent)
Makes a new drop down view to hold the data pointed to by cursor.
abstract View newView ( Context context, Cursor cursor, ViewGroup parent)
Makes a new view to hold the data pointed to by cursor.
Cursor runQueryOnBackgroundThread ( CharSequence constraint)
Runs a query with the specified constraint.
void setFilterQueryProvider ( FilterQueryProvider filterQueryProvider)
Sets the query filter provider used to filter the current Cursor.
Cursor swapCursor ( Cursor newCursor)
Swap in a new Cursor, returning the old Cursor.
Protected Methods
void init ( Context context, Cursor c, boolean autoRequery)
This method was deprecated in API level 11. Don't use this, use the normal constructor. This will be removed in the future.
void onContentChanged ()
Called when the ContentObserver on the cursor receives a change notification.
[Expand]
Inherited Methods
From class android.widget.BaseAdapter
From class java.lang.Object
From interface android.widget.Adapter
From interface android.widget.Filterable
From interface android.widget.ListAdapter
From interface android.widget.SpinnerAdapter

Constants

public static final int FLAG_AUTO_REQUERY

This constant was deprecated in API level 11.
This option is discouraged, as it results in Cursor queries being performed on the application's UI thread and thus can cause poor responsiveness or even Application Not Responding errors. As an alternative, use LoaderManager with a CursorLoader .

If set the adapter will call requery() on the cursor whenever a content change notification is delivered. Implies FLAG_REGISTER_CONTENT_OBSERVER .

Constant Value: 1 (0x00000001)

public static final int FLAG_REGISTER_CONTENT_OBSERVER

If set the adapter will register a content observer on the cursor and will call onContentChanged() when a notification comes in. Be careful when using this flag: you will need to unset the current Cursor from the adapter to avoid leaks due to its registered observers. This flag is not needed when using a CursorAdapter with a CursorLoader .

Constant Value: 2 (0x00000002)

Public Constructors

public CursorAdapter ( Context context, Cursor c)

Added in API level 1

This constructor was deprecated in API level 11.
This option is discouraged, as it results in Cursor queries being performed on the application's UI thread and thus can cause poor responsiveness or even Application Not Responding errors. As an alternative, use LoaderManager with a CursorLoader .

Constructor that always enables auto-requery.

Parameters
context The context
c The cursor from which to get the data.

public CursorAdapter ( Context context, Cursor c, boolean autoRequery)

Added in API level 1

Constructor that allows control over auto-requery. It is recommended you not use this, but instead CursorAdapter(Context, Cursor, int) . When using this constructor, FLAG_REGISTER_CONTENT_OBSERVER will always be set.

Parameters
context The context
c The cursor from which to get the data.
autoRequery If true the adapter will call requery() on the cursor whenever it changes so the most recent data is always displayed. Using true here is discouraged.

public CursorAdapter ( Context context, Cursor c, int flags)

Recommended constructor.

Parameters
context The context
c The cursor from which to get the data.
flags Flags used to determine the behavior of the adapter; may be any combination of FLAG_AUTO_REQUERY and FLAG_REGISTER_CONTENT_OBSERVER .

Public Methods

public abstract void bindView ( View view, Context context, Cursor cursor)

Added in API level 1

Bind an existing view to the data pointed to by cursor

Parameters
view Existing view, returned earlier by newView
context Interface to application's global information
cursor The cursor from which to get the data. The cursor is already moved to the correct position.

public void changeCursor ( Cursor cursor)

Added in API level 1

Change the underlying cursor to a new cursor. If there is an existing cursor it will be closed.

Parameters
cursor The new cursor to be used

public CharSequence convertToString ( Cursor cursor)

Added in API level 1

Converts the cursor into a CharSequence. Subclasses should override this method to convert their results. The default implementation returns an empty String for null values or the default String representation of the value.

Parameters
cursor the cursor to convert to a CharSequence
Returns
  • a CharSequence representing the value

public int getCount ()

Added in API level 1

See Also

public Cursor getCursor ()

Added in API level 1

Returns the cursor.

Returns
  • the cursor.

public View getDropDownView (int position, View convertView, ViewGroup parent)

Added in API level 1

Get a View that displays in the drop down popup the data at the specified position in the data set.

Parameters
position index of the item whose view we want.
convertView the old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view.
parent the parent that this view will eventually be attached to
Returns
  • a View corresponding to the data at the specified position.

public Filter getFilter ()

Added in API level 1

Returns a filter that can be used to constrain data with a filtering pattern.

This method is usually implemented by Adapter classes.

Returns
  • a filter used to constrain data

public FilterQueryProvider getFilterQueryProvider ()

Added in API level 1

Returns the query filter provider used for filtering. When the provider is null, no filtering occurs.

Returns
  • the current filter query provider or null if it does not exist

public Object getItem (int position)

Added in API level 1

public long getItemId (int position)

Added in API level 1

public View getView (int position, View convertView, ViewGroup parent)

Added in API level 1

public boolean hasStableIds ()

Added in API level 1

Indicates whether the item ids are stable across changes to the underlying data.

Returns
  • True if the same id always refers to the same object.

public View newDropDownView ( Context context, Cursor cursor, ViewGroup parent)

Added in API level 1

Makes a new drop down view to hold the data pointed to by cursor.

Parameters
context Interface to application's global information
cursor The cursor from which to get the data. The cursor is already moved to the correct position.
parent The parent to which the new view is attached to
Returns
  • the newly created view.

public abstract View newView ( Context context, Cursor cursor, ViewGroup parent)

Added in API level 1

Makes a new view to hold the data pointed to by cursor.

Parameters
context Interface to application's global information
cursor The cursor from which to get the data. The cursor is already moved to the correct position.
parent The parent to which the new view is attached to
Returns
  • the newly created view.

public Cursor runQueryOnBackgroundThread ( CharSequence constraint)

Added in API level 1

Runs a query with the specified constraint. This query is requested by the filter attached to this adapter. The query is provided by a FilterQueryProvider . If no provider is specified, the current cursor is not filtered and returned. After this method returns the resulting cursor is passed to changeCursor(Cursor) and the previous cursor is closed. This method is always executed on a background thread, not on the application's main thread (or UI thread.) Contract: when constraint is null or empty, the original results, prior to any filtering, must be returned.

Parameters
constraint the constraint with which the query must be filtered
Returns
  • a Cursor representing the results of the new query

public void setFilterQueryProvider ( FilterQueryProvider filterQueryProvider)

Added in API level 1

Sets the query filter provider used to filter the current Cursor. The provider's runQuery(CharSequence) method is invoked when filtering is requested by a client of this adapter.

Parameters
filterQueryProvider the filter query provider or null to remove it

public Cursor swapCursor ( Cursor newCursor)

Swap in a new Cursor, returning the old Cursor. Unlike changeCursor(Cursor) , the returned old Cursor is not closed.

Parameters
newCursor The new cursor to be used.
Returns
  • Returns the previously set Cursor, or null if there wasa not one. If the given new Cursor is the same instance is the previously set Cursor, null is also returned.

Protected Methods

protected void init ( Context context, Cursor c, boolean autoRequery)

Added in API level 1

This method was deprecated in API level 11.
Don't use this, use the normal constructor. This will be removed in the future.

protected void onContentChanged ()

Added in API level 3

Called when the ContentObserver on the cursor receives a change notification. The default implementation provides the auto-requery logic, but may be overridden by sub classes.