zoukankan      html  css  js  c++  java
  • [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对应的视图中.

  • 相关阅读:
    jquery验证手机号码和固定电话号码
    window下安装mongodb及php mongo扩展
    Html form 表单提交前验证
    验证电话号码中间四位加※
    php方法 隐藏手机号中间四位
    win7 64位安装redis 及Redis Desktop Manager使用
    Redis系列-远程连接redis并给redis加锁
    使用密码记录工具keepass来保存密码
    【消息队列MQ】各类MQ比较
    Syslog-ng
  • 原文地址:https://www.cnblogs.com/teamate/p/3413442.html
Copyright © 2011-2022 走看看