[Stephen]自定义SimpleAdapter
作者:AngelDevil 出处:www.cnblogs.com/angeldevil
先看一下构造函数:
public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
参数
context SimpleAdapter关联的View的运行环境
data 一个Map组成的List。在列表中的每个条目对应列表中的一行,每一个map中应该包含所有在from参数中指定的键
resource 一个定义列表项的布局文件的资源ID。布局文件将至少应包含那些在to中定义了的ID
from 一个将被添加到Map映射上的键名
to 将绑定数据的视图的ID,跟from参数对应,这些应该全是TextView
举个例子:

public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         ListView lv = (ListView) findViewById(R.id.listView1);         String[] from = { "Text", "Button" };         int[] to = { R.id.text, R.id.button };         List<Map<String, ?>> list = new ArrayList<Map<String, ?>>();         for (int i = 0; i < 10; i++) {             Map<String, String> m = new HashMap<String, String>();             m.put("Text", "Text" + i);             m.put("Button", "Button" + i);             list.add(m);         }         SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.listitem, from, to);         lv.setAdapter(adapter); }

listitem.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" >
<TextView android:layout_width="wrap_content" android:id="@+id/text" android:layout_height="wrap_content" android:layout_weight="1" />
<Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>

ListView中的每一项都包含一个TextView跟一个Button,在SimpleAdapter的构造函数中,我们指定了要绑定的数据:list, list是一个由Map组成的ArrayList, Map的作用就是连同后面的from, to参数定义数据是如何绑定的,在上面的例子中
String[] from = { "Text", "Button" }; int[] to = { R.id.text, R.id.button };
而在for循环中每个map都put进了两个键值对,键名跟from中定义的一一对应,这就表示对于ListView中的每一项,依次寻找在to参数中定义的资源ID,根据这个资源ID在to参数数组中的位置,找到from参数中对应位置的值,以这个值为键,在list中的相应项(一个Map)中以这个值为键取出这个键对应的值绑定到这个资源ID对应的视图中.
[Stephen]自定义SimpleAdapter的更多相关文章
- Android -- ListView(SimpleAdapter) 自定义适配器
		aaarticlea/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBA ... 
- Android开发(十四)——SimpleAdapter与自定义控件
		ListView中可以使用SimpleAdapter进行数据与视图的绑定,但都是对已有的系统控件的绑定,如果自定义空间直接使用SimpleAdapter绑定,则会报错. 如,使用CircleImage ... 
- android学习日记03--常用控件ListView
		常用控件 8.ListView 列表视图,比如游戏的排行榜.列表数据可以根据屏幕大小自适应 列表的显示需要三个元素: a.ListVeiw:用来展示列表的View. b.适配器:用来把数据映射到Lis ... 
- Android(java)学习笔记133:ListViewProject案例(ListView + BaseAdapter + CheckBox)
		这个案例可能稍微复杂一点,我会讲述详细一点: 1.首先是AndroidManifest.xml: <?xml version="1.0" encoding="utf ... 
- BaseAdapter使listview设置不同背景图片并添加selector
		前段时间为了实现根据item不同的内容实现不同的背景色google了好久只找到了个隔行换色,通过自定义SimpleAdapter终于实现了此功能,但是定义了selector并没有触发点击效果.今天重新 ... 
- Android(java)学习笔记75:ListViewProject案例(ListView + BaseAdapter + CheckBox)
		这个案例可能稍微复杂一点,我会讲述详细一点: 1. 首先是AndroidManifest.xml: <?xml version="1.0" encoding="ut ... 
- 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)
		1.listview入门,自定义的数据适配器 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/and ... 
- Android中ListView同过自定义布局并使用SimpleAdapter的方式实现数据的绑定
		1.listview的数据填充可以通过ArrayAdapter,SimpleAdapter,也可以是一个xml文件,但是ArrayAdapter与SimpleAdapter的区别是: 2 ArrayA ... 
- Android中SimpleAdapter的使用—自定义列表
		本人初学Android,今天研究到Adapter这块感觉挺有意思的,写了个自定义列表进行测试 首先我们新建一个layout列表布局文件,具体布局可以自己设定. 下面贴上我的自定义布局文件代码 < ... 
随机推荐
- log4net 总结
			说实话,我并不是太想写这篇文章,因为我承诺过要完成博客园的部分功能,所以一直都在积极的利用下班时间来完善这个系统, 但是我又不想让看我源代码的朋友不知道我写的代码是什么意思,所以我还是单独写一个文章, ... 
- thinkphp 框架的学习(1) 扩展配置文件
			在config.php里面写入 1:'LOAD_EXT_CONFIG' => array('SETTINGS' => 'settings'); 系统会判断是否有参数:LOAD_EXT_CO ... 
- proteus画元件
			一.元件概述 使用过“protel DXP”画元件的人想必对一个元件的构成已经非常清楚了.一个完整的元件包括如下几个部分: 元件 = 原理图元件模型 + PCB封装模型 + 电路仿真特性 1.原理图元 ... 
- 【@Transactional】Spring 之注解事务 @Transactional
			spring 事务注解 默认遇到throw new RuntimeException("...");会回滚 需要捕获的throw new Exception("...&q ... 
- dtv_driver.ko
			替换dtv_driver.ko .步骤: shell@android:/ # get_rootfs.sh ... 
- 【web安全】第六弹:手工SQL注入详解
			前一段时间,在对SQL注入有了新的理解之后,写了这篇文章.本来准备投稿,因为内容过于基础被打回来了,想想屯着也没意思,发出来发出来~~本来有好多图的,但是博客园发图很麻烦,word文档的链接会贴在文章 ... 
- 一般处理程序(ashx)和页面处理程序(aspx)的区别
			客官请看图 图中的Httphandler就是处理程序. 两者的共同点 如果把aspx处理程序和ashx处理程序放到上图中,他们是处在相同的位置的, 他们都实现了IHttphandler接口.实 ... 
- SQL中not and or优先级问题
			SQL中 not and or优先级问题 刚刚在项目中遇到这样一个问题,SQL语句如下: 我想要的结果的条件是:1. LIBRARY_ID=1 或者 LIB_ID=1 2.STATUS=3 但是结果 ... 
- jquery 简单弹出层(转)
			预定义html代码:没有 所有代码通过js生成和移除. 预定义css /* 基本弹出层样式 */ .my-popup-overlay { width:100%; height:auto; /* wid ... 
- Access forbidden! XAMPP虚拟主机的问题
			XAMPP Control Panel v3.2.1添加虚拟主机出现 Access forbidden! You don't have permission to access the request ... 
