public interface
Adapter
间接子类:
1. ListAdater
public interface ListAdapter implements Adapter
android.widget.ListAdapter
Known Indirect
Subclasses
简介:
ArrayAdapter比较简单,但它只能用于显示文字。
而SimpleAdapter则有很强的扩展性,可以自定义出各种效果。
SimpleCursorAdapter则可以从数据库中读取数据显示在列表上。
通过从写BaseAdapter可以在列表上加处理的事件等。
① ArrayAdapter
构造方法:
public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)
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 |
objects | The objects to represent in the ListView. |
public ArrayAdapter (Context context, int textViewResourceId, List<T> objects)
Constructor
Parameters
context | The current context. |
---|---|
textViewResourceId | The resource ID for a layout file containing a TextView to use when instantiating views. |
objects | The objects to represent in the ListView. |
T[] objects 也可换为 List<T> objects
textViewResourceid:资源id或者 TextView的id,一般布局文件为:
android.R.layout.simple_list_item_single_choice <!-- 带选择的列表 --> android.R.layout.simple_list_item_1 <!-- 显示一个TextView 的列表 -->
实例代码:
ListView listView; private String[] data = { "Android:eoeAndroid.com", "eoeAndroid:", "eoeInstaller", "eoeDouban", "eoeWhere", "eoeInfoAssistant", "eoeDakarGame","eoeTrack" }; ...... listView = new ListView(this); listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data)); listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, data)); listView.setItemsCanFocus(true); listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); setContentView(listView);
② SimpleAdapter
构造方法:
public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
Added in API level 1
Constructor
Parameters
context |
The context where the View associated with this SimpleAdapter is running |
data |
A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from" |
resource |
布局文件,Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to" |
from |
即需要显示的 Map 中的 Key ,A list of column names that will be added to the Map associated with each item. |
to |
from中Key 中对应的 View 的 id ,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. |
参数说明:
from:map中的key
to:map中key对应的value 要显示在Layout中的Views的id。
实例说明:
列表中的内容为 Map 映射的
① 新建 Map<String,Objects> 的 Map 对象,添加 key/value 对。
② 在创建 List<? extends Map<String,Objects>> 的 对象 列表,添加 Map 对象。
③ 把 List 对象 添加到 Adapter 中,
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.list_item, new String[] { "姓名", "性别" }, new int[] {R.id.mview1, R.id.mview2 });④为 ListView 对象添加适配器。
listview.setAdapter(adpter); listview.setOnItemClickListener(listener);
③ BaseAdapter
public abstract classBaseAdapter
extends Object
implements ListAdapterSpinnerAdapter
↳ |
android.widget.BaseAdapter |