Most visited

Recently visited

Added in API level 1

SimpleCursorAdapter

public class SimpleCursorAdapter
extends ResourceCursorAdapter

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


一个简单的适配器,可将游标中的列映射到XML文件中定义的TextView或ImageView。 您可以指定所需的列,要显示列的视图以及定义这些视图外观的XML文件。 绑定发生在两个阶段。 首先,如果SimpleCursorAdapter.ViewBinder可用,则调用setViewValue(android.view.View, android.database.Cursor, int) 如果返回值为true,则发生绑定。 如果返回值为false,并且要绑定的视图是TextView,则调用setViewText(TextView, String) 如果返回值为false,并且要绑定的视图是ImageView,则调用setViewImage(ImageView, String) 如果找不到合适的绑定, IllegalStateException抛出IllegalStateException 如果此适配器用于过滤,例如在AutoCompleteTextView ,则可以使用SimpleCursorAdapter.CursorToStringConverterFilterQueryProvider接口来控制过滤过程。 有关更多信息,请参阅convertToString(android.database.Cursor)runQueryOnBackgroundThread(CharSequence)

Summary

Nested classes

interface SimpleCursorAdapter.CursorToStringConverter

SimpleCursorAdapter的外部客户端可以使用此类来定义Cursor应该如何转换为字符串。

interface SimpleCursorAdapter.ViewBinder

SimpleCursorAdapter的外部客户端可以使用此类将Cursor的值绑定到视图。

Inherited constants

From class android.widget.CursorAdapter
From interface android.widget.Adapter

Public constructors

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

此构造函数在API级别11中已弃用。此选项不鼓励,因为它导致在应用程序的UI线程上执行游标查询,因此可能导致响应性较差甚至出现应用程序无响应错误。 作为替代,使用LoaderManagerCursorLoader

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

标准构造函数。

Public methods

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

将所有传入构造函数的“to”参数的字段名称与它们相应的游标列一起按照“from”参数中的指定进行绑定。

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

更改光标并同时更改列与视图映射。

CharSequence convertToString(Cursor cursor)

返回由当前CursorToStringConverter定义的指定游标的CharSequence表示。

SimpleCursorAdapter.CursorToStringConverter getCursorToStringConverter()

返回用于将过滤游标转换为字符串的转换器。

int getStringConversionColumn()

返回用于获取Cursor的字符串表示的列的索引。

SimpleCursorAdapter.ViewBinder getViewBinder()

返回用于将数据绑定到视图的 SimpleCursorAdapter.ViewBinder

void setCursorToStringConverter(SimpleCursorAdapter.CursorToStringConverter cursorToStringConverter)

设置用于将过滤游标转换为字符串的转换器。

void setStringConversionColumn(int stringConversionColumn)

定义用于获取该Cursor的字符串表示的Cursor中列的索引。

void setViewBinder(SimpleCursorAdapter.ViewBinder viewBinder)

设置用于将数据绑定到视图的联编程序。

void setViewImage(ImageView v, String value)

由bindView()调用以设置ImageView的图像,但前提是没有现有的ViewBinder或现有的ViewBinder无法处理与ImageView的绑定。

void setViewText(TextView v, String text)

只有在没有现有的ViewBinder或现有ViewBinder无法处理与TextView的绑定时,才由bindView()调用以设置TextView的文本。

Cursor swapCursor(Cursor c)

交换一个新的游标,返回旧的游标。

Inherited methods

From class android.widget.ResourceCursorAdapter
From class android.widget.CursorAdapter
From class android.widget.BaseAdapter
From class java.lang.Object
From interface android.widget.Filterable
From interface android.widget.ThemedSpinnerAdapter
From interface android.widget.ListAdapter
From interface android.widget.SpinnerAdapter
From interface android.widget.Adapter

Public constructors

SimpleCursorAdapter

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

此构造函数在API级别11中已弃用。
不鼓励使用此选项,因为它会导致在应用程序的UI线程上执行游标查询,因此可能导致响应性较差甚至出现应用程序无响应错误。 作为替代,使用LoaderManagerCursorLoader

构造函数使能自动重新查询。

Parameters
context Context
layout int
c Cursor
from String
to int

SimpleCursorAdapter

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

标准构造函数。

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(Context, Cursor, int).

Public methods

bindView

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

将所有传入构造函数的“to”参数的字段名称与它们相应的游标列一起按照“from”参数中的指定进行绑定。 绑定发生在两个阶段。 首先,如果SimpleCursorAdapter.ViewBinder可用,则调用setViewValue(android.view.View, android.database.Cursor, int) 如果返回值为true,则发生绑定。 如果返回值为false,并且要绑定的视图是TextView,则调用setViewText(TextView, String) 如果返回的值为false,并且要绑定的视图是ImageView,则调用setViewImage(ImageView, String) 如果找不到合适的绑定, IllegalStateException抛出IllegalStateException

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
void changeCursorAndColumns (Cursor c, 
                String[] from, 
                int[] to)

更改光标并同时更改列与视图映射。

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
CharSequence convertToString (Cursor cursor)

返回由当前CursorToStringConverter定义的指定游标的CharSequence表示。 如果没有设置CursorToStringConverter,则使用字符串转换列。 如果转换列为-1,则如果游标为空或Cursor.toString(),则返回的String为空。

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

返回用于将过滤游标转换为字符串的转换器。

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

也可以看看:

getStringConversionColumn

Added in API level 1
int getStringConversionColumn ()

返回用于获取Cursor的字符串表示的列的索引。

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

也可以看看:

getViewBinder

Added in API level 1
SimpleCursorAdapter.ViewBinder getViewBinder ()

返回用于将数据绑定到视图的 SimpleCursorAdapter.ViewBinder

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

也可以看看:

setCursorToStringConverter

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

设置用于将过滤游标转换为字符串的转换器。

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

也可以看看:

setStringConversionColumn

Added in API level 1
void setStringConversionColumn (int stringConversionColumn)

定义用于获取该Cursor的字符串表示的Cursor中列的索引。 仅当当前的CursorToStringConverter为空时,该列才用于将Cursor转换为字符串。

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

也可以看看:

setViewBinder

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

设置用于将数据绑定到视图的联编程序。

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
void setViewImage (ImageView v, 
                String value)

由bindView()调用以设置ImageView的图像,但前提是没有现有的ViewBinder或现有的ViewBinder无法处理与ImageView的绑定。 默认情况下,该值将被视为图像资源。 如果该值不能用作图像资源,则该值将用作图像Uri。 意图被需要过滤从数据库检索的字符串的适配器覆盖。

Parameters
v ImageView: ImageView to receive an image
value String: the value retrieved from the cursor

setViewText

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

只有在没有现有的ViewBinder或现有ViewBinder无法处理与TextView的绑定时,才由bindView()调用以设置TextView的文本。 意图被需要过滤从数据库检索的字符串的适配器覆盖。

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

swapCursor

Added in API level 11
Cursor swapCursor (Cursor c)

交换一个新的游标,返回旧的游标。 changeCursor(Cursor)不同,返回的旧游标关闭。

Parameters
c Cursor: The new cursor to be used.
Returns
Cursor 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.

Hooray!