SimpleCursorAdapter

public class SimpleCursorAdapter extends ResourceCursorAdapter


Static library support version of the framework's android.widget.SimpleCursorAdapter. Used to write apps that run on platforms prior to Android 3.0. When running on Android 3.0 or above, this implementation is still used; it does not try to switch to the framework's implementation. See the framework SDK documentation for a class overview.

Summary

Nested types

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

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

Public constructors

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

This method 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.

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

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

SimpleCursorAdapter.CursorToStringConverter

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

int

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

SimpleCursorAdapter.ViewBinder

Returns the ViewBinder used to bind data to views.

void

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

Sets the binder used to bind data to views.

void

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

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

Swap in a new Cursor, returning the old Cursor.

Inherited Constants

From android.widget.Adapter
static final int
static final int
NO_SELECTION = -2147483648
From androidx.cursoradapter.widget.CursorAdapter
static final int

This field 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.

static final int

If set the adapter will register a content observer on the cursor and will call onContentChanged when a notification comes in.

Inherited methods

From android.widget.Adapter
abstract int
abstract Object
getItem(int p)
abstract long
getItemId(int p)
abstract View
getView(int p, View p1, ViewGroup p2)
From android.widget.BaseAdapter
boolean
CharSequence[]
View
getDropDownView(int position, View convertView, ViewGroup parent)
int
getItemViewType(int position)
int
boolean
boolean
boolean
isEnabled(int position)
void
void
void
void
setAutofillOptions(CharSequence[] options)
void
From androidx.cursoradapter.widget.CursorAdapter
void

Change the underlying cursor to a new cursor.

int
Cursor

Returns the cursor.

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

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
void
init(Context context, Cursor c, boolean autoRequery)

This method is deprecated.

Don't use this, use the normal constructor.

void

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

Cursor

Runs a query with the specified constraint.

void

Sets the query filter provider used to filter the current Cursor.

From android.widget.Filterable
abstract Filter
From androidx.cursoradapter.widget.ResourceCursorAdapter
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
setViewResource(int layout)

Sets the layout resource of the item views.

Public constructors

SimpleCursorAdapter

Added in 1.0.0
Deprecated in 1.0.0
public SimpleCursorAdapter(
    Context context,
    int layout,
    Cursor c,
    String[] from,
    int[] to
)

Constructor the enables auto-requery.

SimpleCursorAdapter

Added in 1.0.0
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

int layout

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"

Cursor c

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

String[] from

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

int[] to

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.

int flags

Flags used to determine the behavior of the adapter, as per CursorAdapter.

Public methods

bindView

Added in 1.1.0-alpha01
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 android.widget.SimpleCursorAdapter.ViewBinder is available, setViewValue 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 is invoked. If the returned value is false and the view to bind is an ImageView, setViewImage is invoked. If no appropriate binding can be found, an IllegalStateException is thrown.

Throws
java.lang.IllegalStateException

if binding cannot occur

changeCursorAndColumns

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

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

Parameters
Cursor c

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

String[] from

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

int[] to

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

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 1.0.0
public SimpleCursorAdapter.CursorToStringConverter getCursorToStringConverter()

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

getStringConversionColumn

Added in 1.0.0
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 1.0.0
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 1.0.0
public void setCursorToStringConverter(
    SimpleCursorAdapter.CursorToStringConverter cursorToStringConverter
)

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

Parameters
SimpleCursorAdapter.CursorToStringConverter cursorToStringConverter

the Cursor to String converter, or null to remove the converter

setStringConversionColumn

Added in 1.0.0
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
int stringConversionColumn

a valid index in the current Cursor or -1 to use the default conversion mechanism

setViewBinder

Added in 1.0.0
public void setViewBinder(SimpleCursorAdapter.ViewBinder viewBinder)

Sets the binder used to bind data to views.

Parameters
SimpleCursorAdapter.ViewBinder viewBinder

the binder used to bind data to views, can be null to remove the existing binder

setViewImage

Added in 1.0.0
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
ImageView v

ImageView to receive an image

String value

the value retrieved from the cursor

setViewText

Added in 1.0.0
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
TextView v

TextView to receive text

String text

the text to be set for the TextView

swapCursor

public Cursor swapCursor(Cursor newCursor)

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

Parameters
Cursor newCursor

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.