[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列表布局文件,具体布局可以自己设定. 下面贴上我的自定义布局文件代码 < ...
随机推荐
- mysql---字符集详解
常用的字符集包括ASCII ,GB2312 , GBK , UTF-8 ,Unicode 首先要知道 ASCII编码: 用一个字节来标识0-9的数字.大小写字母.及一些标点和不可见字符.1个字节8位, ...
- jQuery.hhNewSilder 滚动图片插件
/** * jQuery.hhNewSilder 滚动图片插件 * User: huanhuan * QQ: 651471385 * Email: th.wanghuan@gmail.com ...
- google map android api v2
我在这主要列举几个需要注意的问题: 1.需要注意使用的api版本的问题,例如google map android api v1就和v2差别很大,包括申请key方面,所以在搜索资料的时候一定注意版本问题 ...
- AngularJS(2)-Scope作用域和控制器
$scope: 根作用域 所有的应用都有一个 $rootScope,它可以作用在 ng-app 指令包含的所有 HTML 元素中. $rootScope 可作用于整个应用中.是各个 controlle ...
- 查找计算机IP及占用端口
1. 在电脑启动搜索框,输入cmd回车打开命令提示符窗口. 输入ipconfig,就可以查看电脑的子网淹没,默认网关,IP等信息. 2. 查看本机开放的端口,即已被占用的端口号. 命令: netsta ...
- 几种更新(Update语句)查询的方法【转】
正 文: 数据库更新就一种方法Update,其标准格式:Update 表名 set 字段=值 where 条件不过根据数据的来源不同,还是有所区别的: 1.从外部输入这种比较简单例:update t ...
- Python 知识点
1. generator #g is a generator and g is iterable g = (x*x for x in range(5)) for n in g: print(n) # ...
- 解决WP8应用里ListBox绑定数据变多导致越来越卡
ListBox控件绑定数据,当滑动到底部的时候加载数据到列表上,这样就会产生一个问题,当ListBox上面绑定的数据有几千条的时候,界面将会卡顿,我们可以通过在ListBox上只绑定指定数量的数据,其 ...
- oracle srvctl 命令
SRVCTL命令可以控制RAC数据库中的instance,listener以及services. 通常SRVCTL在ORACLE用户下执行.下面我们来介绍srvctl命令. 1.通过SRVCTL命令来 ...
- loadrunner 脚本和replaylog中的中文乱码问题(转载)
解决这个问题必须认识到一个事实就是,loadrunner和测试服务器交换数据使用的是utf8格式,但是展现在replaylog中是使用gb2312格式,而且在脚本中如何使用web_reg_find的时 ...