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( ...
随机推荐
- bean装配--注解
1,Dao层 package com.songyan.zhujie; public interface UserDao { public void say(); } package com.songy ...
- JDBC_PreparedStatement 防sql注入
package songyan.jdbc.login.prepared; import java.sql.Connection; import java.sql.DriverManager; impo ...
- PC-LINT
静态代码检查工具PC-Lint(一) 2006-12-07 23:50 C/C++语言的语法拥有其它语言所没有的灵活性,这种灵活性带来了代码效率的提升,但相应增加了代码中存在隐患的可能性.静态代码 ...
- Python-使用Magellan进行数据匹配总结
参考:http://www.biggorilla.org/zh-hans/walkt/ 使用Magellan进行数据匹配过程如下: 假设有两个数据源为A和B, A共有四列数据:(A_Column1,A ...
- curl 执行post请求
#curl -l -H "Content-type: application/json;charset=UTF-8" -H "X-Forwarded-For: 20.20 ...
- redis打开非英文存储显示问题
使用jedis 在redis中如果存储非英文的值,入 中文,印地语,马拉蒂语,泰米尔语等.在执行get 或者 hget的时候查询出来的数据会以16进制显示.如何显示原有的值 在redis启动的时候加上 ...
- 一分钟学awk够用
[转载于58同城沈剑] 1.什么是AWK(1)Aho.Weinberger.Kernighan三位发明者名字首字母:(2)一个行文本处理工具: 2.AWK基本原理2.1原理:逐行处理文件中的数据 2. ...
- 在ecshop中添加页面,并且实现后台管理
后台一共需要修改下面的四个文件 admin/template.php admin/includes/lib_template.php languages/zh_cn/admin/template.ph ...
- 2017.3.31 spring mvc教程(四)全局的异常处理
学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...
- 转:阿里 Weex 思路与实战(web相关)
Weex——关于移动端动态性的思考.实现和未来 2016-04-05 勾股.伊耆 移动开发前线 本文由手机淘宝技术团队赵锦江(勾股).黄金涌(伊耆)等专家创作.手淘作为电商应用,对客户端/前端的动态性 ...