SimpleAdapter的用法
学习listView的时候,按照例子设定item的布局为系统提供的simple_list_item_single_choice.xml@frameworks/base/core/res/res/layout/ 加上SimpleAdapter,感觉很爽,什么都不用写直接就用了,然后我就自己定义了一个布局一个ImageView和一个CheckedTextView,问题来了,点击不选中,但是使用SimpleAdapter的时候可是好好的。我决定肯定是SimpleAdapter做了什么事情可以很好的实现单选(后来发现与simpleadapter完全无关,只是CheckedTextView实现了选择功能)。于我决定认真的研究一下SimpleAdapter。
SimpleAdapter继承于BaseAdapter,代码也不多@frameworks/base/core/java/android/widget/SimpleAdapter.java
最最重要的一个函数是bindView,它被getView调用。它个是它使用拿数据适配组件的关键
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");
}
}
}
}
}
注意我做成红色标记的这四个点,暂时不管第四个,先说说前三个。
在此之前先看一个SimpleAdapter的用法
new SimpleAdapter(getActivity(),
buildData(),R.layout.logo,
new String[]{"img","text","check"},
new int[]{R.id.logo, R.id.tvListItem,R.id.tvListItem});
红色标记①是给实现Checkable接口的TextView赋值,所以它检查值的类型,如果是boolean它就明白是设置成选中状态,如果是TextView就是赋字符值。所以要给CheckedTextView赋值,需要写两次就像我上面的例子一样text和check一个是显示的内容,一个是批选中状态,所以下面我写了两次R.id.tvListItem。
除些之后它还可以给ImageView赋值,所就是说只要你的控件是由CheckedTextView,TextView和ImageView中的一种或者几种组合而成,不管个数是多少个,都可以用SimpleAdapter做数据适配。
再看④,这就更牛B了,先看一下ViewBinder的定义
public static interface ViewBinder {
boolean setViewValue(View view, Object data, String textRepresentation);
}
在不用自己实现Adapter的情况下,自定义数据的适配,simpleAdapter的适用范围又扩大了很多,简直是万能的了。自定义一个控件,然后自己实现viewBinder接口,设置到SimpleAdapter中,就可以用它来适配你自己的控件了。
SimpleAdapter的用法的更多相关文章
- ArrayAdapter、SimpleAdapter简单用法
1. 使用流程 2. ArrayAdapter new ArrayAdapter<?>(context, textViewResourceId, objects) context:上下 ...
- SimpleAdapter ArrayAdapter用法
listView = (ListView) findViewById(R.id.list_main); /* String[] strings = {"A","A&quo ...
- BaseAdapter,SimpleAdapter,CursorAdapter的用法
简单好用的Adapter---ArrayAdapter ArrayAdapter是BaseAdapter的派生类,在BaseAdapter的基础上,添加了一项重大的功能:可以直接使用泛型构造. 我们先 ...
- ListView蛮好用
知识点如下: 1. ListView的基本用法 2. ArrayAdapter和SimpleAdapter的用法 3. OnScrollListener 和 OnItemClickListener 4 ...
- Android 自学之列表视图ListView和ListActivity
ListView是手机系统中使用非常广泛的一种组件,它以垂直列表的形式显示所有列表项. 创建ListView有两种方式: 直接使用ListView创建. 让Activity继承ListActivity ...
- 与数据库打交道的Adapter----SimpleCursorAdapter
http://www.cnblogs.com/wenjiang/p/3196486.html 程序员是这个世界上最神奇的职业,因为几乎所有其他职业的人都能转到该行来,只要他智力正常,有接受过正规的编程 ...
- android入门 — ListView
ListView主要是用来解决大量数据展示的问题,它的用途很广泛,几乎所有的app都会用到,比如说知乎.今日头条.微博.通讯录等. ListView允许用户通过上下滑动的方式将屏幕外的数据滚动到屏幕中 ...
- Android——ListView优化
1.ListView基本概念 列表显示需要三个元素: ListView:用来展示列表的View. 适配器:用来把数据映射到ListView上 数据:具体的将被映射的字符串,图片或基本组件 适配器类型分 ...
- Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法与有用代码片段(转)
摘自:http://blog.csdn.net/shakespeare001/article/details/7926783 Adapter是连接后端数据和前端显示的适配器接口,是数据Data和UI( ...
随机推荐
- linux 自动删除n天前文件
现在系统每天生成一个日期文件夹,并压缩上传到ftp服务器,造成目录下文件太多,所以决定写个定时删除文件的任务 写脚本文件 find /home/data -mtime +90 -name " ...
- RS-232
RS-232 锁定 同义词 rs232一般指RS-232 本词条由“科普中国”百科科学词条编写与应用工作项目 审核 . 个人计算机上的通讯接口之一,由电子工业协会(Electronic Industr ...
- ios unit test 工程选择release时候报错Undefined symbols for architecture i386
Undefined symbols for architecture i386: "_OBJC_CLASS_$_ItemReturn", referenced from: objc ...
- 【mybatis】in查询+判断list查询条件是否进行in查询
mybatis中的in查询: 并且判断in查询的list是否为null或者list有值才进行In查询 <select id="find" parameterType=&quo ...
- Linux命令基本格式
1 起始符td@td-Lenovo-IdeaPad-Y410P:~$ 第一个td表示当前登录管理员名,中间@无实际意义,td-Lenovo-IdeaPad-Y410P表示主机名,-表示当前所在目录(h ...
- selenium _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0
在关闭driver时,如果用close,而不是用quit,会出现如下错误: Exception ignored in: <bound method Popen.__del__ of <su ...
- HTML5游戏实战(1):50行代码实现正面跑酷游戏
前段时间看到一个"熊来了"的HTML5跑酷游戏,它是一个典型的正面2D跑酷游戏,这里借用它来介绍一下用Gamebuilder+CanTK开发正面跑酷游戏的基本方法. CanTK(C ...
- 弄清java中的字节与字符
问题 在java中,一个字符等于多少字节? 或者更详细的问:在java中,一个英文字符等于多少字节?一个中文字符等于多少字节? 答案 Java采用unicode来表示字符,java中的一个char是2 ...
- oracle数据库中函数和存储过程中的区别
一.函数必须有返回值,过程没有返回值: 二.函数可以单独执行,过程必须通过execute执行: 三.函数可以嵌入SQL中执行,过程不能. 可以将比较复杂的查询写成函数,然后在过程中调用.
- 2017.5.15 markdown简明教程
0.说明 markdown是一种书写格式,html是一种发布格式.markdown的语法种类只对应html标记的一小部分(只涵盖纯文本). 不在markdown涵盖范围的标签,都可以直接在文档里用ht ...