SimpleAdapter的参数说明

第一个参数 表示访问整个android应用程序接口,基本上所有的组件都需要
 第二个参数表示生成一个Map(String ,Object)列表选项
 第三个参数表示界面布局的id  表示该文件作为列表项的组件
 第四个参数表示该Map对象的哪些key对应value来生成列表项
 第五个参数表示来填充的组件 Map对象key对应的资源一依次填充组件 顺序有对应关系
 注意的是map对象可以key可以找不到 但组件的必须要有资源填充  因为 找不到key也会返回null 其实就相当于给了一个null资源
 下面的程序中如果 new String[] { "name", "head", "desc","name" } new int[] {R.id.name,R.id.head,R.id.desc,R.id.head}
这个head的组件会被name资源覆盖

这个key就是第四个参数,而第四个参数表示去list里面拿数据,第五个参数表示拿到的数据显示在那个组件中;第三个参数只是一个布局的xml文件;xml文件中有多个组件,这些组件显示什么数据 就是后面两个参数的作用

代码
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="horizontal"
  6. tools:context=".MainActivity" >
  7. <ListView
  8. android:id="@+id/lt1"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content" >
  11. </ListView>
  12. </LinearLayout>
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="horizontal" >
  6. <ImageView
  7. android:id="@+id/head"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:paddingLeft="10dp" />
  11. <LinearLayout
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:orientation="vertical" >
  15. <TextView
  16. android:id="@+id/name"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:textSize="20dp"
  20. android:textColor="#f0f"
  21. android:paddingLeft="10dp"/>
  22. <TextView
  23. android:id="@+id/desc"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:textSize="14dp"
  27. android:paddingLeft="10dp"/>
  28. </LinearLayout>
  29. </LinearLayout>
    1. package com.example.simpleadptertest;
    2. import java.util.ArrayList;
    3. import java.util.HashMap;
    4. import java.util.List;
    5. import java.util.Map;
    6. import android.app.Activity;
    7. import android.os.Bundle;
    8. import android.view.Menu;
    9. import android.widget.ListView;
    10. import android.widget.SimpleAdapter;
    11. public class MainActivity extends Activity {
    12. private String[] name = { "剑萧舞蝶", "张三", "hello", "诗情画意" };
    13. private String[] desc = { "魔域玩家", "百家执行", "高级的富一代", "妹子请过来..一个善于跑妹子的。。" };
    14. private int[] imageids = { R.drawable.libai, R.drawable.nongyu,
    15. R.drawable.qingzhao, R.drawable.tiger };
    16. private ListView lt1;
    17. @Override
    18. protected void onCreate(Bundle savedInstanceState) {
    19. super.onCreate(savedInstanceState);
    20. setContentView(R.layout.activity_main);
    21. List<Map<String, Object>> listems = new ArrayList<Map<String, Object>>();
    22. for (int i = 0; i < name.length; i++) {
    23. Map<String, Object> listem = new HashMap<String, Object>();
    24. listem.put("head", imageids[i]);
    25. listem.put("name", name[i]);
    26. listem.put("desc", desc[i]);
    27. listems.add(listem);
    28. }
    29. /*SimpleAdapter的参数说明
    30. * 第一个参数 表示访问整个android应用程序接口,基本上所有的组件都需要
    31. * 第二个参数表示生成一个Map(String ,Object)列表选项
    32. * 第三个参数表示界面布局的id  表示该文件作为列表项的组件
    33. * 第四个参数表示该Map对象的哪些key对应value来生成列表项
    34. * 第五个参数表示来填充的组件 Map对象key对应的资源一依次填充组件 顺序有对应关系
    35. * 注意的是map对象可以key可以找不到 但组件的必须要有资源填充  因为 找不到key也会返回null 其实就相当于给了一个null资源
    36. * 下面的程序中如果 new String[] { "name", "head", "desc","name" } new int[] {R.id.name,R.id.head,R.id.desc,R.id.head}
    37. * 这个head的组件会被name资源覆盖
    38. * */
    39. SimpleAdapter simplead = new SimpleAdapter(this, listems,
    40. R.layout.simple_item, new String[] { "name", "head", "desc" },
    41. new int[] {R.id.name,R.id.head,R.id.desc});
    42. lt1=(ListView)findViewById(R.id.lt1);
    43. lt1.setAdapter(simplead);
    44. }
    45. @Override
    46. public boolean onCreateOptionsMenu(Menu menu) {
    47. // Inflate the menu; this adds items to the action bar if it is present.
    48. getMenuInflater().inflate(R.menu.main, menu);
    49. return true;
    50. }
    51. }

转载《SimpleAdapter的参数说明》的更多相关文章

  1. 转载《Android LayoutInflater详解》

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  2. Android LayoutInflater详解

      在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且 ...

  3. Android LayoutInflater详解(转)

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  4. Android LayoutInflater详解 (转)

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  5. Android——LayoutInflater详解

    在实际工作中,事先写好的布局文件往往不能满足我们的需求,有时会根据情况在代码中自定义控件,这就需要用到LayoutInflater. LayoutInflater在Android中是"扩展& ...

  6. <转> Android LayoutInflater详解

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  7. [ 转载 ] Android设计模式详解

    从Android再来认识23种设计模式 ReadyShow 关注  0.2 2018.01.06 23:18* 字数 3855 阅读 2584评论 0喜欢 20 概况来看本文章的内容 创建型:5个 单 ...

  8. Android Notification 详解(一)——基本操作

    Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...

  9. Android Notification 详解——基本操作

    Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...

  10. Android ActionBar详解

    Android ActionBar详解 分类: Android2014-04-30 15:23 1094人阅读 评论(0) 收藏 举报 androidActionBar   目录(?)[+]   第4 ...

随机推荐

  1. Android中的Service小结

    简介 Service适合执行不需要和用户交互,而且长期运行的任务.即使程序被切换回后台,服务仍然可以正常运行.Service并不自动开启线程,默认运行在主线程中. Service中需要重载的函数 on ...

  2. sql 2008 修改链接服务器 Rpc &Rpc Out

    From: http://blog.csdn.net/gnolhh168/article/details/41725873 USE [master] GO EXEC master.dbo.sp_ser ...

  3. windows8.1下php环境搭建及基本配置(php+apache+mysql)

    一.php下载安装:php-5.6.1-Win32-VC11-x64.zip.解压,操作: 1.复制php.ini-production,更名为php.ini 2.在环境变量PATH末尾添加:D:\p ...

  4. php 数据库select函数//待编辑

    <?php $con = mysql_connect("localhost","root","root"); if (!$con) { ...

  5. 使用Volley执行网络数据传输

    首先需要实例化一个RequestQueue RequestQueue queue = Volley.newRequestQueue(this); 然后是根据提供的URL请求字符串响应 String u ...

  6. 【转帖】Python在大数据分析及机器学习中的兵器谱

    Flask:Python系的轻量级Web框架. 1. 网页爬虫工具集 Scrapy 推荐大牛pluskid早年的一篇文章:<Scrapy 轻松定制网络爬虫> Beautiful Soup ...

  7. [转]as3 算法实例【输出1 到最大的N 位数 题目:输入数字n,按顺序输出从1 最大的n 位10 进制数。比如输入3,则输出1、2、3 一直到最大的3 位数即999。】

    思路:如果我们在数字前面补0的话,就会发现n位所有10进制数其实就是n个从0到9的全排列.也就是说,我们把数字的每一位都从0到9排列一遍,就得到了所有的10进制数. /** *ch 存放数字 *n n ...

  8. windows下python Tkinner环境布置(包含PIL环境安装)

    布置步骤:1.安装python 2.7.11 安装步骤:由于网上存在有相关经验,所以在此引用一下 http://jingyan.baidu.com/article/0bc808fc42dfab1bd4 ...

  9. EXCEL里面的数字显示为文本 不用科学计数法显示

    1. 在输入这一串数字前加撇号“'”(英文状态下的单引号)即可.2. 先将这一列设置为“文本”格式,然后直接输入这一串数字即可.   已经输入好了数字,那估计你这些数字的后三位都已经全变成“0”了,用 ...

  10. java 终端练习

    Java第一天笔记 一.Window中常见的dos命令 在哪里操作dos命令: Win7 ---> 开始  ---->所有程序--->附件---->命令提示符 Win7--&g ...