android.support.v4.widget
Class SimpleCursorAdapter

java.lang.Object
  extended by android.widget.BaseAdapter
      extended by android.support.v4.widget.CursorAdapter
          extended by android.support.v4.widget.ResourceCursorAdapter
              extended by android.support.v4.widget.SimpleCursorAdapter
All Implemented Interfaces:
android.widget.Adapter, android.widget.Filterable, android.widget.ListAdapter, android.widget.SpinnerAdapter

public class SimpleCursorAdapter
extends ResourceCursorAdapter

Static library support version of the framework's 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.


Nested Class Summary
static interface SimpleCursorAdapter.CursorToStringConverter
          This class can be used by external clients of SimpleCursorAdapter to define how the Cursor should be converted to a String.
static interface SimpleCursorAdapter.ViewBinder
          This class can be used by external clients of SimpleCursorAdapter to bind values fom the Cursor to views.
 
Field Summary
protected  int[] mFrom
          A list of columns containing the data to bind to the UI.
protected  int[] mTo
          A list of View ids representing the views to which the data must be bound.
 
Fields inherited from class android.support.v4.widget.CursorAdapter
FLAG_AUTO_REQUERY, FLAG_REGISTER_CONTENT_OBSERVER, mAutoRequery, mChangeObserver, mContext, mCursor, mCursorFilter, mDataSetObserver, mDataValid, mFilterQueryProvider, mRowIDColumn
 
Fields inherited from interface android.widget.Adapter
IGNORE_ITEM_VIEW_TYPE, NO_SELECTION
 
Constructor Summary
SimpleCursorAdapter(android.content.Context context, int layout, android.database.Cursor c, String[] from, int[] to)
          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(android.content.Context context, int layout, android.database.Cursor c, String[] from, int[] to, int flags)
          Standard constructor.
 
Method Summary
 void bindView(android.view.View view, android.content.Context context, android.database.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(android.database.Cursor c, String[] from, int[] to)
          Change the cursor and change the column-to-view mappings at the same time.
 CharSequence convertToString(android.database.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 SimpleCursorAdapter.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(android.widget.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(android.widget.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 an TextView.
 android.database.Cursor swapCursor(android.database.Cursor c)
          Swap in a new Cursor, returning the old Cursor.
 
Methods inherited from class android.support.v4.widget.ResourceCursorAdapter
newDropDownView, newView, setDropDownViewResource, setViewResource
 
Methods inherited from class android.support.v4.widget.CursorAdapter
changeCursor, getCount, getCursor, getDropDownView, getFilter, getFilterQueryProvider, getItem, getItemId, getView, hasStableIds, init, onContentChanged, runQueryOnBackgroundThread, setFilterQueryProvider
 
Methods inherited from class android.widget.BaseAdapter
areAllItemsEnabled, getItemViewType, getViewTypeCount, isEmpty, isEnabled, notifyDataSetChanged, notifyDataSetInvalidated, registerDataSetObserver, unregisterDataSetObserver
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

mFrom

protected int[] mFrom
A list of columns containing the data to bind to the UI. This field should be made private, so it is hidden from the SDK.


mTo

protected int[] mTo
A list of View ids representing the views to which the data must be bound. This field should be made private, so it is hidden from the SDK.

Constructor Detail

SimpleCursorAdapter

@Deprecated
public SimpleCursorAdapter(android.content.Context context,
                                      int layout,
                                      android.database.Cursor c,
                                      String[] from,
                                      int[] to)
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.


SimpleCursorAdapter

public SimpleCursorAdapter(android.content.Context context,
                           int layout,
                           android.database.Cursor c,
                           String[] from,
                           int[] to,
                           int flags)
Standard constructor.

Parameters:
context - The context where the ListView associated with this SimpleListItemFactory is running
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"
c - The database cursor. Can be null if the cursor is not available yet.
from - A list of column names representing the data to bind to the UI. Can be null if the cursor is not available yet.
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.
flags - Flags used to determine the behavior of the adapter, as per CursorAdapter.CursorAdapter(Context, Cursor, int).
Method Detail

bindView

public void bindView(android.view.View view,
                     android.content.Context context,
                     android.database.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, SimpleCursorAdapter.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(TextView, String) is invoked. If the returned value is false and the view to bind is an ImageView, setViewImage(ImageView, String) is invoked. If no appropriate binding can be found, an IllegalStateException is thrown.

Specified by:
bindView in class CursorAdapter
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.
Throws:
IllegalStateException - if binding cannot occur
See Also:
CursorAdapter.bindView(android.view.View, android.content.Context, android.database.Cursor), getViewBinder(), #setViewBinder(android.widget.SimpleCursorAdapter.ViewBinder), setViewImage(ImageView, String), setViewText(TextView, String)

getViewBinder

public SimpleCursorAdapter.ViewBinder getViewBinder()
Returns the SimpleCursorAdapter.ViewBinder used to bind data to views.

Returns:
a ViewBinder or null if the binder does not exist
See Also:
bindView(android.view.View, android.content.Context, android.database.Cursor), #setViewBinder(android.widget.SimpleCursorAdapter.ViewBinder)

setViewBinder

public void setViewBinder(SimpleCursorAdapter.ViewBinder viewBinder)
Sets the binder used to bind data to views.

Parameters:
viewBinder - the binder used to bind data to views, can be null to remove the existing binder
See Also:
bindView(android.view.View, android.content.Context, android.database.Cursor), getViewBinder()

setViewImage

public void setViewImage(android.widget.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 to receive an image
value - the value retrieved from the cursor

setViewText

public void setViewText(android.widget.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 an TextView. Intended to be overridden by Adapters that need to filter strings retrieved from the database.

Parameters:
v - TextView to receive text
text - the text to be set for the TextView

getStringConversionColumn

public int getStringConversionColumn()
Return the index of the column used to get a String representation of the Cursor.

Returns:
a valid index in the current Cursor or -1
See Also:
CursorAdapter.convertToString(android.database.Cursor), setStringConversionColumn(int), #setCursorToStringConverter(android.widget.SimpleCursorAdapter.CursorToStringConverter), getCursorToStringConverter()

setStringConversionColumn

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 - a valid index in the current Cursor or -1 to use the default conversion mechanism
See Also:
CursorAdapter.convertToString(android.database.Cursor), getStringConversionColumn(), #setCursorToStringConverter(android.widget.SimpleCursorAdapter.CursorToStringConverter), getCursorToStringConverter()

getCursorToStringConverter

public SimpleCursorAdapter.CursorToStringConverter getCursorToStringConverter()
Returns the converter used to convert the filtering Cursor into a String.

Returns:
null if the converter does not exist or an instance of SimpleCursorAdapter.CursorToStringConverter
See Also:
#setCursorToStringConverter(android.widget.SimpleCursorAdapter.CursorToStringConverter), getStringConversionColumn(), setStringConversionColumn(int), CursorAdapter.convertToString(android.database.Cursor)

setCursorToStringConverter

public void setCursorToStringConverter(SimpleCursorAdapter.CursorToStringConverter cursorToStringConverter)
Sets the converter used to convert the filtering Cursor into a String.

Parameters:
cursorToStringConverter - the Cursor to String converter, or null to remove the converter
See Also:
#setCursorToStringConverter(android.widget.SimpleCursorAdapter.CursorToStringConverter), getStringConversionColumn(), setStringConversionColumn(int), CursorAdapter.convertToString(android.database.Cursor)

convertToString

public CharSequence convertToString(android.database.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().

Overrides:
convertToString in class CursorAdapter
Parameters:
cursor - the Cursor to convert to a CharSequence
Returns:
a non-null CharSequence representing the cursor

swapCursor

public android.database.Cursor swapCursor(android.database.Cursor c)
Description copied from class: CursorAdapter
Swap in a new Cursor, returning the old Cursor. Unlike CursorAdapter.changeCursor(Cursor), the returned old Cursor is not closed.

Overrides:
swapCursor in class CursorAdapter
Parameters:
c - 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.

changeCursorAndColumns

public void changeCursorAndColumns(android.database.Cursor c,
                                   String[] from,
                                   int[] to)
Change the cursor and change the column-to-view mappings at the same time.

Parameters:
c - The database cursor. Can be null if the cursor is not available yet.
from - A list of column names representing the data to bind to the UI. Can be null if the cursor is not available yet.
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.


Copyright © 2013 Marek Kedzierski. All Rights Reserved.