SimpleAdapter的基本使用

来源:互联网 发布:linux chmod start.sh 编辑:程序博客网 时间:2024/06/01 13:14
继承BaseAdapter写适配器多了,一般都不喜欢在使用sdk中自定的简单的适配器了。这里总结一下系统自带的SimpleAdapter的使用;

API文档中有说明,并且可以看出SimpleAdapter是继承了BaseAdapter并从中简化了一些接口调用。

public class SimpleAdapter extends BaseAdapter implements Filterable 

An easy adapter to map static data to views defined in an XML file. You can specify the data backing the list as an ArrayList of Maps. Each entry in the ArrayList corresponds to one row in the list. The Maps contain the data for each row. You also specify an XML file that defines the views used to display the row, and a mapping from keys in the Map to specific views. Binding data to views occurs in two phases. First, if a SimpleAdapter.ViewBinder is available, setViewValue(android.view.View, Object, String) is invoked. If the returned value is true, binding has occurred. If the returned value is false, the following views are then tried in order: 

大概就是:使用给定的XML布局文件建立一个简单的适配器。你需要用给定的数据对应布局中的控件。

那么关键是getView函数了,看源码:

public View getView(int position, View convertView, ViewGroup parent) {        return createViewFromResource(position, convertView, parent, mResource);    }private View createViewFromResource(int position, View convertView,            ViewGroup parent, int resource) {        View v;        if (convertView == null) {            v = mInflater.inflate(resource, parent, false);        } else {            v = convertView;        }        bindView(position, v);        return v;    }private void bindView(int position, View view) {final Map dataSet = mData.get(position);if (dataSet == null) {    return;}final ViewBinder binder = mViewBinder;final String[] from = mFrom;final int[] to = mTo;final int count = to.length;for (int i = 0; i < count; i++) {    final View v = view.findViewById(to[i]);    if (v != null) {final Object data = dataSet.get(from[i]);String text = data == null ? "" : data.toString();if (text == null) {    text = "";}boolean bound = false;if (binder != null) {    bound = binder.setViewValue(v, data, text);}if (!bound) {    if (v instanceof Checkable) {if (data instanceof Boolean) {    ((Checkable) v).setChecked((Boolean) data);} else if (v instanceof TextView) {    // Note: keep the instanceof TextView check at the bottom of these    // ifs since a lot of views are TextViews (e.g. CheckBoxes).    setViewText((TextView) v, text);} else {    throw new IllegalStateException(v.getClass().getName() +    " should be bound to a Boolean, not a " +    (data == null ? "<unknown type>" : data.getClass()));}    } else if (v instanceof TextView) {// Note: keep the instanceof TextView check at the bottom of these// ifs since a lot of views are TextViews (e.g. CheckBoxes).setViewText((TextView) v, text);    } else if (v instanceof ImageView) {if (data instanceof Integer) {    setViewImage((ImageView) v, (Integer) data);                            } else {    setViewImage((ImageView) v, text);}    } else {throw new IllegalStateException(v.getClass().getName() + " is not a " +" view that can be bounds by this SimpleAdapter");    }}    }}}


可以看出其中支持的类型也比较少,基本上支持了几种常见类型(ImageView,Button,CheckBox,TextView),所以如果需要更多控件类型甚至是自定义类型的话,相信还是继承BaseAdapter重写里面的方法作为一个自定义适配器使用较为适合。

接下来就是基本的使用了:

listView = (ListView) findViewById(R.id.building_monitor_popout_listview);int size = 30;List<Map<String, String>> list;list = new ArrayList<Map<String, String>>(size);for (int i = 0; i < size; i++) {Map<String, String> m = new HashMap<String, String>();m.put("text", "item"+i);list.add(m);}SimpleAdapter simpleAdapter = new SimpleAdapter(context, list,R.layout.item_adapter, new String[] { "text" },new int[] { R.id.textView });listView.setAdapter(simpleAdapter);

其中from和to两个数组要对应一直即可;


item_adapter.xml文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/textView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:layout_margin="5dp"        android:textSize="18sp" /></LinearLayout>

其中SImpleAdapter的构造函数说明:

public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)(Added in API level 1)

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
A list of column names that will be added to the Map associated with each item. 
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.  

如果使用更为简单的适配器可以BaseAdapter下面的ArrayAdapter;


ArrayAdapter的使用:

String[] strs = {"Item1","Item2","Item3","Item4","Item5"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
AlarmActivity.this,
android.R.layout.simple_expandable_list_item_1,
strs);

比较BaseAdapter下面的几个类:

BaseAdapter是一个抽象类,继承它需要实现较多的方法,所以也就具有较高的灵活性;
ArrayAdapter支持泛型操作,最为简单,只能展示一行字。
SimpleAdapter有最好的扩充性,可以自定义出各种效果。
SimpleCursorAdapter可以适用于简单的纯文字型ListView,它需要Cursor的字段和UI的id对应起来。如需要实现更复杂的UI也可以重写其他方法。可以认为是SimpleAdapter对数据库的简单结合,可以方便地把数据库的内容以列表的形式展示出来。

SimpleAdapter的扩展性最好,可以定义各种各样的布局出来。 

可以看出ArrayAdapter没有SimpleAdapter的扩展性强,ArrayAdapter是固定的难以扩展。

来自csdn博客:http://blog.csdn.net/dreamintheworld/article/details/42551347




0 0
原创粉丝点击