zoukankan      html  css  js  c++  java
  • 学习Android中的Adapter

    Android中的Adapter在自定义显示列表时非常有用,比如SimpleAdapter,它的构造函数是:

    public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

    它的各参数的意思: 

    1.context,上下文,SimpleAdapter关联的视图,一般而言就是当前的Activity,this

    2.data,泛型的List,如ArrayList,Map或者HashMap

    3.resource,资源文件,一个R.layout,就是要显示的布局

    4.from ,一个数组,Map中的键值对。

    5.to,layout的xml文件中命名id形成的唯一的int型标识符

    比如:

    在一个ListActivity中定义一个List:

    List<Map<String,String>> people= new ArrayList<Map<String, String>>(); 

    Map<String,String> m=new HashMap<String,String>();
    m.put("name","tom");
    m.put("age","20");

    people.add(m); 

    ... 

    SimpleAdapter adapter = new SimpleAdapter(this,
    (List<Map<String,String>>) feets, R.layout.main,
    new String[] { "name","age" }, new int[] {R.id.name,R.id.age });
    setListAdapter(adapter); 

    其中:

    R.id.name,R.id.age 是在一个XML布局文件中定义的两个用于显示name和age的TextView。布局文件中要有一个ListView。或者在程序中定义也可以。

    另外,注意在ListActivity中不需要设置setContentView,系统被自动加载。 

  • 相关阅读:
    java调用打印机方式二
    java调用系统打印机
    Centos7开放端口(永久)
    java毫秒级别定时器
    java计算接口调用时间
    java实现当前时间往前推N小时
    java注解日志记录到数据库
    Java后端HttpClient Post提交文件流 及服务端接收文件流
    springboot整合websocket
    注解@Slf4j使用
  • 原文地址:https://www.cnblogs.com/GarfieldTom/p/2019607.html
Copyright © 2011-2022 走看看