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. HDU 2669

    题目大意: 已知线性方程ax+by=1; 输入a, b的值, 要求输出整数解x, y的值(输出x, y的最小整数解), 若没有解, 输出"sorry".   分析: 求线性方程的解 ...

  2. 每天一点 js join 函数

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  3. shell 简单计算脚本

  4. js之事件冒泡和事件捕获详细介绍

    (1)冒泡型事件:事件按照从最特定的事件目标到最不特定的事件目标(document对象)的顺序触发. IE 5.5: div -> body -> document IE 6.0: div ...

  5. .NET3.5项目转.NET2.0项目技巧

    最近有一个项目,一开始开发是用VS2008(.NET3.5)开发的,该项目是一个Windorm客户端软件,由于在大规模的推广过程中,发现在安装.NET3.5Framework的时候浪费了太多时间,而且 ...

  6. ant打包webservice jar

    <project name="helloworldservice" basedir="." default="deploy"> ...

  7. 【转】java中volatile关键字的含义

    java中volatile关键字的含义   在java线程并发处理中,有一个关键字volatile的使用目前存在很大的混淆,以为使用这个关键字,在进行多线程并发处理的时候就可以万事大吉. Java语言 ...

  8. 龙哥库塔法or欧拉法求解微分方程matlab实现

    举例:分别用欧拉法和龙哥库塔法求解下面的微分方程 我们知道的欧拉法(Euler)"思想是用先前的差商近似代替倒数",直白一些的编程说法即:f(i+1)=f(i)+h*f(x,y)其 ...

  9. OPENCV配置 C1083: 无法打开包括文件:“opencv2/opencv.hpp”

    本人没实操过C/C++,理论看过不少. 目前出于提升技术的考虑想做OPENCV 配置了2天,总算是配置好了..遇到不少问题. 计算机配置WIN10,用户环境变量配置也郁闷过... JDK等配置方式,配 ...

  10. 学习SVG系列(2):SVG图形系列

    SVG形状: 矩形 <rect> 圆形 <circle> 椭圆 <ellipse> 线 <line> 折线 <polyline> 多边形 & ...