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( ...
随机推荐
- PL/SQL分支、循环语句
CREATE OR REPLACE PROCEDURE PR_TEST2 IS V_CASE NUMBER(5) := 100; BEGIN IF 2 < 1 THEN DBMS_OUTPUT. ...
- 每天一个liunx命令4之 ps -ef ,ps -aux ,ps aux
1ps aux和ps –aux 请注意"ps -aux"不同于"ps aux".POSIX和UNIX的标准要求"ps -aux"打印用户名为 ...
- Ubuntu 16.04使用“从互联网自动获取”时间无法写入硬件BIOS的奇怪问题
目前发现的就是这个问题,只能手动同步到BIOS. 如果是手动设置过时间,那么可以正常同步到BIOS. 而如果切换到从互联网自动获取时间时,是不能同步到BIOS的,但是界面上的时间确实最新的. 并且这个 ...
- Volley缓存说明——一个请求两次回调
从上一篇文章Android 异步网络请求框架-Volley了解volley的一些出来过程,当然也包含网络请求和缓存处理的流程,但是在此需要单独做一些说明. 我在使用过程中忽略了一个事情,就是一个网络请 ...
- php设计模式之建造者模式
建造者模式 建造者设计模式的目的是消除其他对象的复杂创建过程.使用建造者设计模式不仅是最佳的做法,而且在摸个对象的构造和配置方法改变时候,可以尽可能的减少重复更改代码. <?php /** *p ...
- ES6里关于函数的拓展(二)
一.构造函数 Function构造函数是JS语法中很少被用到的一部分,通常我们用它来动态创建新的函数.这种构造函数接受字符串形式的参数,分别为函数参数及函数体 var add = new Functi ...
- memcache运行机制(转)
网上其实有很多文章说明了memcached是如何运作的,特别是底层的内存分配是如何运作的.我参考过很多资料,比较有启发意义的有几个: 首先是官方的英文资料,虽然文章太多.很难看懂,我个人觉得说得也不是 ...
- [转载]使用java.lang.Process类的简单例子
FROM: http://segmentfault.com/blog/lidonghao/1190000000372192 ProcessBuilder类是J2SE 1.5在java.lang中新添加 ...
- es6 webpack转es5
更新时间: 2018-7-31 首次更新. 先生成package.json npm init -y 再安装以下npm插件 npm i babel-core babel-loader babel-pre ...
- sql习题练习
表结构: create database MyCompany go use MyCompany go create table Departments ( Department_ID ,) prima ...