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. Log4J详解

    Log4J 简介  Log4j有三个主要的组件:Loggers(记录器),Appenders (输出源)和Layouts(布局).这里可简单理解为日志类别,日志要输出的地方和日志以何种形式输出.综合使 ...

  2. React Native 一个组件styles BUG

    'use strict'; var React = require('react-native'); var { StyleSheet, PanResponder, View, Text } = Re ...

  3. GridView获取列子段的几种途径

    GridView是ASP.NET中功能强大的数据显示控件,它的RowDataBound事件为我们提供了方便的控制行.列数据的途径. 要获取当前行的某个数据列,我在实践中总结有如下几种方法: 1. Ce ...

  4. Ubuntu Install Java

    http://linuxpilot.com/ubuntu-java class HelloWorld{public static void main(String[]arg){System.out.p ...

  5. JAVA中SERIALVERSIONUID的解释

    serialVersionUID作用:        序列化时为了保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性.有两种生成方式:       一个是默认的1L,比如:private st ...

  6. RHEL6 --部署phpMyAdmin与论坛系统

    一.rpm安装LAMP平台部署phpMyAdmin 二.搭建wordpress个人博客系统 三.搭建论坛系统 一.rpm安装LAMP平台及部署phpMyAdmin 1.phpMyAdmin简介及获取方 ...

  7. testlink安装

    今天安装了一下testlink,完全按照高峻博客里的做法,最后安装成功了 遇到的问题: 问题表现: 新安装TestLink,登录Testlink后,新建一个项目后,会出现如下提示: There are ...

  8. GridView不能添加头布局,并且scrollView与GridView冲突导致一些页面无法融合

    此贴为标记贴 方便下次使用 在项目需求中原本是用ScrollView来进行整个页面的滑动,ScrollView里面包含的有图片轮播,文字轮播,与2列GridView的item 问题 使用原生的Grid ...

  9. BootStrap框架

    简介: Bootstrap,来自 Twitter,是目前很受欢迎的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的,它简洁灵活,使得 Web 开发更加快捷,是一个CSS ...

  10. vim ---- 一键自动indent的命令

    当用vim拷贝某一段代码到另一个程序的时候,往往indent会有一些问题.. 下面这个强大的命令能够让你一键让代码有很好的格式. gg=G 例子: