在Android中,ListView是用来显示一个列表的控件。每一行列表都是一个独立的元素。这种控件既可以方便的显示从系统中其他应用读取出来的数据,也可独立的为各行元素设置监听器。
根据API文档中的说明,使用ListView显示控件的基本流程如下:
1. 将所要显示的数据以ArrayList,Cursor或者Array形式封装成为对象。
eg1:
|
eg2.
|
Cursor一般用于读取其他应用程序的制定的数据。读取完毕后,Cursor指向所得结果集的第一项数据。
|
2. 将所得到的数据加入到各自类型对应的适配器中。
2.1 ArrayList型
使用SimpleAdapter作为适配器
public ArrayAdapter (Context context, int resource, int textViewResourceId)
Constructor
Parameters
context | The current context. |
---|---|
resource | The resource ID for a layout file containing a layout to use when instantiating views. |
textViewResourceId | The id of the TextView within the layout resource to be populated |
eg.
|
2.2 ContentProvider型
使用SimpleCursorAdapter作为适配器
public SimpleCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to)
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. |
eg.
|
2.3 数组型
使用ArrayAdapter
public ArrayAdapter (Context context, int resource, int textViewResourceId)
Constructor
Parameters
context | The current context. |
---|---|
resource | The resource ID for a layout file containing a layout to use when instantiating views. |
textViewResourceId | The id of the TextView within the layout resource to be populated
|
转载于:https://blog.51cto.com/52android/464112