学习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的用法的更多相关文章

  1. ArrayAdapter、SimpleAdapter简单用法

    1. 使用流程 2. ArrayAdapter new ArrayAdapter<?>(context, textViewResourceId, objects)   context:上下 ...

  2. SimpleAdapter ArrayAdapter用法

    listView = (ListView) findViewById(R.id.list_main); /* String[] strings = {"A","A&quo ...

  3. BaseAdapter,SimpleAdapter,CursorAdapter的用法

    简单好用的Adapter---ArrayAdapter ArrayAdapter是BaseAdapter的派生类,在BaseAdapter的基础上,添加了一项重大的功能:可以直接使用泛型构造. 我们先 ...

  4. ListView蛮好用

    知识点如下: 1. ListView的基本用法 2. ArrayAdapter和SimpleAdapter的用法 3. OnScrollListener 和 OnItemClickListener 4 ...

  5. Android 自学之列表视图ListView和ListActivity

    ListView是手机系统中使用非常广泛的一种组件,它以垂直列表的形式显示所有列表项. 创建ListView有两种方式: 直接使用ListView创建. 让Activity继承ListActivity ...

  6. 与数据库打交道的Adapter----SimpleCursorAdapter

    http://www.cnblogs.com/wenjiang/p/3196486.html 程序员是这个世界上最神奇的职业,因为几乎所有其他职业的人都能转到该行来,只要他智力正常,有接受过正规的编程 ...

  7. android入门 — ListView

    ListView主要是用来解决大量数据展示的问题,它的用途很广泛,几乎所有的app都会用到,比如说知乎.今日头条.微博.通讯录等. ListView允许用户通过上下滑动的方式将屏幕外的数据滚动到屏幕中 ...

  8. Android——ListView优化

    1.ListView基本概念 列表显示需要三个元素: ListView:用来展示列表的View. 适配器:用来把数据映射到ListView上 数据:具体的将被映射的字符串,图片或基本组件 适配器类型分 ...

  9. Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法与有用代码片段(转)

    摘自:http://blog.csdn.net/shakespeare001/article/details/7926783 Adapter是连接后端数据和前端显示的适配器接口,是数据Data和UI( ...

随机推荐

  1. 归档 & 解档

    代码实现 遵守协议 class AccessToken: NSObject, NSCoding 实现协议方法 // MARK: - 归档&解档 required init(coder aDec ...

  2. 搭建基于asp.net的wcf服务,ios客户端调用的实现记录

    一.写wcf 问题: 1.特定的格式 2.数据绑定 3.加密解密 二.发布到iis 问题: 1.访问权限问题,添加everyone权限 访问网站时:http://localhost/WebbUploa ...

  3. 了解RxJava以及如何在Android应用中使用它

    如果你在阅读这篇文章,相信你一定很想了解RxJava以及如何在Android应用中使用它.可能你已经见过RxJava的代码了,但仍然有些疑惑,愿你能在这篇文章里找到答案. 当我第一次使用RxJava的 ...

  4. 通过Linux定时任务实现定时轮询数据库及发送Http请求

    通过Linux定时任务实现定时轮询数据库及发送Http请求 概述 有时需要临时增加一个定时任务(需要根据数据库查询结果然后发送HTTP请求),如果在项目中额外增加(Java+Spring+Quartz ...

  5. Java三大器之监听器(Listener)的工作原理和代码演示

    现在来说说Servlet的监听器Listener,它是实现了javax.servlet.ServletContextListener 接口的服务器端程序,它也是随web应用的启动而启动,只初始化一次, ...

  6. 使用Ant构建简单项目

    Apache Ant主要用于Java项目的构建.为什么要使用Ant: 1)项目构建不是简单的编译,各种资源需要合理构建,例如有的类需要压缩成jar文件,有的文件需要放在指定位置,有时候需要使用配置文件 ...

  7. 关于angularjs dom渲染结束再执行的问题

    情景 当我点击了button, div才能显示.并且我想知道这个div的高度. 问题 当我点击这个button以后这个.chrome就然告诉我这个div高度是0.这不是睁着眼睛说瞎话吗?怎么能如此欺骗 ...

  8. 基于React的贪吃蛇游戏的设计与实现

    代码地址如下:http://www.demodashi.com/demo/11818.html 贪吃蛇小游戏(第二版) 一年半前层用react写过贪吃蛇小游戏https://github.com/ca ...

  9. chrome使用

    本文转载于http://www.cnblogs.com/tester-l/p/5743031.html Chrome调试工具各个工具的作用: Element Elements板块你可以看到整个页面的D ...

  10. Objective-C中的关联(objc_setAssociatedObject,objc_getAssociatedObject,objc_removeAssociatedObjects)

    关联的概念 所谓的关联,字面意思是把两个相关的对象放在一起,实际也是如此.把两个对象相互关联起来,使得其中的一个对象成为另外一个对象的一部分,这就是关联. 关联的作用 使用Category,我们可以给 ...