SimpleAdapter初步理解

SimpleAdapter初步理解。

谷歌官方文档: 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:
If no appropriate binding can be found, an  IllegalStateException  is thrown.

翻译过来的大概意思就是:一个简单的适配器,用来给定义在XML文件内的视图匹配数据,你可以用list指定这数据支持就好像一个Maps的list一样,每一个Arraylist中的条目都正确对应list中的一行,Maps包含了每一行的数据,你同样要指定一个XML文件用来明确显示这行的视图,并且用key从Map中取得的mapping去指定视图。给视图绑定数据分两段,首先,如果一个SimpleAdapter.ViewBinder是可用的,setViewValue(android.view.View, Object, String)被调用。如果返回的值是true,绑定成功,如果返回值是false,接着尝试下列视图。
如果没有合适的绑定,那么会抛出异常。

看得有点云里雾里。。接着看构造方法吧。。
SimpleAdapter( Context context,  List<? extends  Map< String, ?>> data, int resource,  String[] from, int[] to)
这是它的构造方法,可以看到,需要传入一个上下文,一个list,一个resource,一个String数组,一个int数组。我们也不知道传进去的这些东西都有什么用,接着往下看。

Parameters:
contextthe context where the View associated with this SimpleAdapter is running。

data:AList of Maps,Each entry in the List corresponds to one row in the list,The Maps contain the data for each row, and shouldincludeall the entries specified in "from"

resourceResource 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".

fromA list of column names that will be added to the Map associated with each item.

toThe 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.

context:SimpleAdapter运行时候的视图的上下文;

Data:一个Maps列表,列表中的每一行都匹配List中的每一个Entry,Maps包含每一行的数据,并且包含指定在From中的所有entries。

Resourse:定义了列表条目的views的资源布局标识符,布局文件应该至少包含一个定义在"to"中的视图。

from:被加到Map的名字列的一个列表,和每一个Item都有关。

to:视图应该展示在from中的参数列,应该全部都是TextViews,列表里的第一批N个视图被from参数里列表的N列赋值。


其实谷歌官方给的to的解释有些牵强,官方给的是These should all be TextViews,但其实ImageView也可以。

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");
    }
}

这是官方源码,可以看到当View是Image的时候,也是可以的。






 
 






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值