SimpleCursorAdapter


public class SimpleCursorAdapter
extends ResourceCursorAdapter

java.lang.Object
   ↳ android.widget.BaseAdapter
     ↳ android.widget.CursorAdapter
       ↳ android.widget.ResourceCursorAdapter
         ↳ android.widget.SimpleCursorAdapter


An easy adapter to map columns from a cursor to TextViews or ImageViews defined in an XML file. You can specify which columns you want, which views you want to display the columns, and the XML file that defines the appearance of these views. Binding occurs in two phases. First, if a SimpleCursorAdapter.ViewBinder is available, ViewBinder.setViewValue(android.view.View, android.database.Cursor, int) is invoked. If the returned value is true, binding has occured. If the returned value is false and the view to bind is a TextView, setViewText(android.widget.TextView, java.lang.String) is invoked. If the returned value is false and the view to bind is an ImageView, setViewImage(android.widget.ImageView, java.lang.String) is invoked. If no appropriate binding can be found, an IllegalStateException is thrown. If this adapter is used with filtering, for instance in an AutoCompleteTextView, you can use the SimpleCursorAdapter.CursorToStringConverter and the FilterQueryProvider interfaces to get control over the filtering process. You can refer to convertToString(android.database.Cursor) and CursorAdapter.runQueryOnBackgroundThread(java.lang.CharSequence) for more information.

Summary

Nested classes

interface SimpleCursorAdapter.CursorToStringConverter

This class can be used by external clients of SimpleCursorAdapter to define how the Cursor should be converted to a String. 

interface SimpleCursorAdapter.ViewBinder

This class can be used by external clients of SimpleCursorAdapter to bind values fom the Cursor to views. 

Inherited constants

int FLAG_AUTO_REQUERY

This constant was deprecated in API level 15. 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.

int IGNORE_ITEM_VIEW_TYPE

An item view type that causes the AdapterView to ignore the item view.

int NO_SELECTION

Public constructors

SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to)

This constructor is deprecated. 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.

SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags)

Standard constructor.

Public methods

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

Binds all of the field names passed into the "to" parameter of the constructor with their corresponding cursor columns as specified in the "from" parameter.

void changeCursorAndColumns(Cursor c, String[] from, int[] to)

Change the cursor and change the column-to-view mappings at the same time.

CharSequence convertToString(Cursor cursor)

Returns a CharSequence representation of the specified Cursor as defined by the current CursorToStringConverter.

SimpleCursorAdapter.CursorToStringConverter getCursorToStringConverter()

Returns the converter used to convert the filtering Cursor into a String.

int getStringConversionColumn()

Return the index of the column used to get a String representation of the Cursor.

SimpleCursorAdapter.ViewBinder getViewBinder()

Returns the ViewBinder used to bind data to views.

void setCursorToStringConverter(SimpleCursorAdapter.CursorToStringConverter cursorToStringConverter)

Sets the converter used to convert the filtering Cursor into a String.

void setStringConversionColumn(int stringConversionColumn)

Defines the index of the column in the Cursor used to get a String representation of that Cursor.

void setViewBinder(SimpleCursorAdapter.ViewBinder viewBinder)

Sets the binder used to bind data to views.

void setViewImage(ImageView v, String value)

Called by bindView() to set the image for an ImageView but only if there is no existing ViewBinder or if the existing ViewBinder cannot handle binding to an ImageView.

void setViewText(TextView v, String text)

Called by bindView() to set the text for a TextView but only if there is no existing ViewBinder or if the existing ViewBinder cannot handle binding to a TextView.

Cursor swapCursor(Cursor c)

Swap in a new Cursor, returning the old Cursor.

Inherited methods

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

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

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

Inflates view(s) from the specified XML file.

void setDropDownViewResource(int dropDownLayout)

Sets the layout resource of the drop down views.

void setDropDownViewTheme(Resources.Theme theme)

Sets the Resources.Theme against which drop-down views are inflated.

void setViewResource(int layout)

Sets the layout resource of the item views.

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()

How many items are in the data set represented by this Adapter.

Cursor getCursor()

Returns the cursor.

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

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

Resources.Theme getDropDownViewTheme()

Returns the value previously set by a call to setDropDownViewTheme(android.content.res.Resources.Theme).

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)

Get the data item associated with the specified position in the data set.

long getItemId(int position)

Get the row id associated with the specified position in the list.

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

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

boolean hasStableIds()

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

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

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

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.

void onContentChanged()

Called when the ContentObserver on the cursor receives a change notification.

Cursor runQueryOnBackgroundThread(CharSequence constraint)

Runs a query with the specified constraint.

void setDropDownViewTheme(Resources.Theme theme)

Sets the Resources.Theme against which drop-down views are inflated.

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.

boolean areAllItemsEnabled()

Indicates whether all the items in this adapter are enabled.

CharSequence[] getAutofillOptions()

Gets a string representation of the adapter data that can help AutofillService autofill the view backed by the adapter.

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

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

int getItemViewType(int position)

Get the type of View that will be created by getView(int, View, ViewGroup) for the specified item.

int getViewTypeCount()

Returns the number of types of Views that will be created by getView(int, View, ViewGroup).

boolean hasStableIds()

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

boolean isEmpty()
boolean isEnabled(int position)

Returns true if the item at the specified position is not a separator.

void notifyDataSetChanged()

Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

void notifyDataSetInvalidated()

Notifies the attached observers that the underlying data is no longer valid or available.

void registerDataSetObserver(DataSetObserver observer)

Register an observer that is called when changes happen to the data used by this adapter.

void setAutofillOptions(CharSequence... options)

Sets the value returned by getAutofillOptions()

void unregisterDataSetObserver(DataSetObserver observer)

Unregister an observer that has previously been registered with this adapter via registerDataSetObserver(DataSetObserver).

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.

abstract Filter getFilter()

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

abstract Resources.Theme getDropDownViewTheme()

Returns the value previously set by a call to setDropDownViewTheme(android.content.res.Resources.Theme).

abstract void setDropDownViewTheme(Resources.Theme theme)

Sets the Resources.Theme against which drop-down views are inflated.

abstract boolean areAllItemsEnabled()

Indicates whether all the items in this adapter are enabled.

abstract boolean isEnabled(int position)

Returns true if the item at the specified position is not a separator.

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

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

default CharSequence[] getAutofillOptions()

Gets a string representation of the adapter data that can help AutofillService autofill the view backed by the adapter.

abstract int getCount()

How many items are in the data set represented by this Adapter.

abstract Object getItem(int position)

Get the data item associated with the specified position in the data set.

abstract long getItemId(int position)

Get the row id associated with the specified position in the list.

abstract int getItemViewType(int position)

Get the type of View that will be created by getView(int, View, ViewGroup) for the specified item.

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

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

abstract int getViewTypeCount()

Returns the number of types of Views that will be created by getView(int, View, ViewGroup).

abstract boolean hasStableIds()

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

abstract boolean isEmpty()
abstract void registerDataSetObserver(DataSetObserver observer)

Register an observer that is called when changes happen to the data used by this adapter.

abstract void unregisterDataSetObserver(DataSetObserver observer)

Unregister an observer that has previously been registered with this adapter via registerDataSetObserver(DataSetObserver).

Public constructors

SimpleCursorAdapter

Added in API level 1
public SimpleCursorAdapter (Context context, 
                int layout, 
                Cursor c, 
                String[] from, 
                int[] to)

This constructor is deprecated.
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 the enables auto-requery.

Parameters
context Context

layout int

c Cursor

from String

to int

SimpleCursorAdapter

Added in API level 11
public SimpleCursorAdapter (Context context, 
                int layout, 
                Cursor c, 
                String[] from, 
                int[] to, 
                int flags)

Standard constructor.

Parameters
context Context: The context where the ListView associated with this SimpleListItemFactory is running

layout int: resource identifier of a layout file that defines the views for this list item. The layout file should include at least those named views defined in "to"

c Cursor: The database cursor. Can be null if the cursor is not available yet.

from String: A list of column names representing the data to bind to the UI. Can be null if the cursor is not available yet.

to int: The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter. Can be null if the cursor is not available yet.

flags int: Flags used to determine the behavior of the adapter, as per CursorAdapter.CursorAdapter(Context, Cursor, int).

Public methods

bindView

Added in API level 1
public void bindView (View view, 
                Context context, 
                Cursor cursor)

Binds all of the field names passed into the "to" parameter of the constructor with their corresponding cursor columns as specified in the "from" parameter. Binding occurs in two phases. First, if a SimpleCursorAdapter.ViewBinder is available, ViewBinder.setViewValue(android.view.View, android.database.Cursor, int) is invoked. If the returned value is true, binding has occured. If the returned value is false and the view to bind is a TextView, setViewText(android.widget.TextView, java.lang.String) is invoked. If the returned value is false and the view to bind is an ImageView, setViewImage(android.widget.ImageView, java.lang.String) is invoked. If no appropriate binding can be found, an IllegalStateException is thrown.

Parameters
view View: Existing view, returned earlier by newView

context Context: Interface to application's global information

cursor Cursor: The cursor from which to get the data. The cursor is already moved to the correct position.

Throws
IllegalStateException if binding cannot occur

changeCursorAndColumns

Added in API level 3
public void changeCursorAndColumns (Cursor c, 
                String[] from, 
                int[] to)

Change the cursor and change the column-to-view mappings at the same time.

Parameters
c Cursor: The database cursor. Can be null if the cursor is not available yet.

from String: A list of column names representing the data to bind to the UI. Can be null if the cursor is not available yet.

to int: The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter. Can be null if the cursor is not available yet.

convertToString

Added in API level 1
public CharSequence convertToString (Cursor cursor)

Returns a CharSequence representation of the specified Cursor as defined by the current CursorToStringConverter. If no CursorToStringConverter has been set, the String conversion column is used instead. If the conversion column is -1, the returned String is empty if the cursor is null or Cursor.toString().

Parameters
cursor Cursor: the Cursor to convert to a CharSequence

Returns
CharSequence a non-null CharSequence representing the cursor

getCursorToStringConverter

Added in API level 1
public SimpleCursorAdapter.CursorToStringConverter getCursorToStringConverter ()

Returns the converter used to convert the filtering Cursor into a String.

Returns
SimpleCursorAdapter.CursorToStringConverter null if the converter does not exist or an instance of SimpleCursorAdapter.CursorToStringConverter

getStringConversionColumn

Added in API level 1
public int getStringConversionColumn ()

Return the index of the column used to get a String representation of the Cursor.

Returns
int a valid index in the current Cursor or -1

getViewBinder

Added in API level 1
public SimpleCursorAdapter.ViewBinder getViewBinder ()

Returns the ViewBinder used to bind data to views.

Returns
SimpleCursorAdapter.ViewBinder a ViewBinder or null if the binder does not exist

setCursorToStringConverter

Added in API level 1
public void setCursorToStringConverter (SimpleCursorAdapter.CursorToStringConverter cursorToStringConverter)

Sets the converter used to convert the filtering Cursor into a String.

Parameters
cursorToStringConverter SimpleCursorAdapter.CursorToStringConverter: the Cursor to String converter, or null to remove the converter

setStringConversionColumn

Added in API level 1
public void setStringConversionColumn (int stringConversionColumn)

Defines the index of the column in the Cursor used to get a String representation of that Cursor. The column is used to convert the Cursor to a String only when the current CursorToStringConverter is null.

Parameters
stringConversionColumn int: a valid index in the current Cursor or -1 to use the default conversion mechanism

setViewBinder

Added in API level 1
public void setViewBinder (SimpleCursorAdapter.ViewBinder viewBinder)

Sets the binder used to bind data to views.

Parameters
viewBinder SimpleCursorAdapter.ViewBinder: the binder used to bind data to views, can be null to remove the existing binder

setViewImage

Added in API level 1
public void setViewImage (ImageView v, 
                String value)

Called by bindView() to set the image for an ImageView but only if there is no existing ViewBinder or if the existing ViewBinder cannot handle binding to an ImageView. By default, the value will be treated as an image resource. If the value cannot be used as an image resource, the value is used as an image Uri. Intended to be overridden by Adapters that need to filter strings retrieved from the database.

Parameters
v ImageView: ImageView to receive an image

value String: the value retrieved from the cursor

setViewText

Added in API level 1
public void setViewText (TextView v, 
                String text)

Called by bindView() to set the text for a TextView but only if there is no existing ViewBinder or if the existing ViewBinder cannot handle binding to a TextView. Intended to be overridden by Adapters that need to filter strings retrieved from the database.

Parameters
v TextView: TextView to receive text

text String: the text to be set for the TextView

swapCursor

Added in API level 11
public Cursor swapCursor (Cursor c)

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

Parameters
c Cursor: The new cursor to be used.

Returns
Cursor Returns the previously set Cursor, or null if there was not one. If the given new Cursor is the same instance is the previously set Cursor, null is also returned.