zoukankan      html  css  js  c++  java
  • 【Android自学日记】两种适配器的使用

    ArrayAdapter适配器:
    (1)用于显示基本的文字内容
    (2)基本使用过程:新建适配器---创建或加载数据源---适配器加载数据源---视图加载适配器
    ArrayAdapter(上下文,当前ListView加载的每一个列表项所对应的布局文件,数据源)
    (ArrayAdapter)数据适配器的实现过程:
    1.新建适配器

    arr_adapter=new ArrayAdapter<String>(context,布局文件,数据源)

    arr_adapter=new ArrayAdapter<String>(上下文(this),当前ListView加载的每一个列表项所对应的布局文件(android.R.layout.simple_list_item_1),数据源(String[]arr_data={"1","2","3","4"}));
    2.添加数据源到适配器(上一步已包含)
    3.视图(ListView)加载适配器
    listView.setAdapter(arr_adapter);

     

    使用SimpleAdapter

    private ListView listView;

    private SimpleAdapter simple_adapter;

    //声明适配器

    private List<Map<String, Object>> dataList;
    dataList=new ArrayList<Map<String,Object>>();
    //1、新建一个数据适配器—SimpleAdapter();

    //2、适配器加载数据源
    simple_adapter = new SimpleAdapter(this,getData()*获取数据源*,R.layout.布局文件名,new String[]{"键值名1","键值名2"},new int[]{R.id.控件名1,R.id.控件名2});

     

    //3、视图(ListView)加载适配器
    listView.setAdapter(simple_adapter);

     

    // 生成数据源
    private List<Map<String, Object>> getData() {
    for(int i=0;i<20;i++){
    Map<String,Object> map=new HashMap<String,Object>();
    map.put("键值名",R.drawable.ic_launcher);
    map.put("键值名","测试文本"+i);
    dataList.add(map);
    }
    return dataList;
    }

    注意:map.put("pic",R.drawable.ic_launcher);map.put("text","测试文本"+i);这里的pic和text与new int[] { R.id.pic,R.id.text}无关(只是同名罢了),是关联于new String[] {"pic", "text"}中的名字。

     

  • 相关阅读:
    Python pip 下载速度慢? Windows 设置 国内源,用阿里云国内镜像加速
    Go timer 是如何被调度的?
    Go sync.Pool 浅析
    一次错误使用 go-cache 导致出现的线上问题
    golang面向对象分析
    一文完全掌握 Go math/rand
    这一次,彻底搞懂 Go Cond
    面试题:让你捉摸不透的 Go reslice
    当 Go struct 遇上 Mutex
    这可能是最容易理解的 Go Mutex 源码剖析
  • 原文地址:https://www.cnblogs.com/sev7en-/p/5927996.html
Copyright © 2011-2022 走看看