您的位置:首页 > 其它

SimpleAdapter初步理解

2016-07-23 14:54 309 查看

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:

A view that implements Checkable (e.g. CheckBox). The expected bind value is a boolean.
TextView. The expected bind value is a string and 
setViewText(TextView,
String)
 is invoked.
ImageView. The expected bind value is a resource id or a string and 
setViewImage(ImageView,
int)
 or 
setViewImage(ImageView,
String)
 is invoked.
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,接着尝试下列视图。

实现了Checkable的视图,绑定值是布尔值。
TextView,绑定值是字符串类型,并且setViewText(TextView, String)被调用。
ImageView,绑定值是资源ID或者一串字符,并且
setViewImage(ImageView,
int)
 or 
setViewImage(ImageView,
String)
被调用。
如果没有合适的绑定,那么会抛出异常。

看得有点云里雾里。。接着看构造方法吧。。

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

Parameters:
context:the 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"

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.

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的时候,也是可以的。


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: