zoukankan      html  css  js  c++  java
  • ListView 的理解

    如果不太懂下边就将ListView,大家注意看。

    ListView

    上边已经展示过它运行的效果了,这里就不展示运行效果了。

    那么要使用ListView需要哪些步骤呢?举一个例子,可能不太恰当

    冰箱里没有鸡蛋了,我们从家里提了一个篮子去超市买鸡蛋。就是这样的一个过程。我们来分解下这个步骤

    冰箱 == 展示数据 == ListView

    超市里的鸡蛋 == 数据 == ArrayList<E> 泛型集合

    子 == 适配器 == SimpleAdapter

    我们应该将 鸡蛋(ArrayList<E>) 装到 篮子里(SimpleAdapter) 然后提回家 放到 冰箱里( ListView)

    分解完步骤后,那么我们看看如何用代码实现这个过程。

    ListView userList; //声明一个ListView对象(冰箱)

    userList = (ListView)findViewById(R.id.listUserInfo); //获取布局文件中的ListView控件赋值给ListView对象

    ArrayList<HashMap<String, String>> listData = new ArrayList<HashMap<String,String>>(); //数据源 (超市装鸡蛋的盒子)

    HashMap<String, String> hmItem = new HashMap<String, String>(); //需要一个HashMap键值对 (每一个鸡蛋)
      hmItem.put("userName", "张三"); 
      hmItem.put("userPhone", "1234567890");
      listData.add(hmItem); //将鸡蛋装到数据源中

    String[] s = new String[2]; //列 和键值对中的键 一一对应 每个键值对应该是一样的列数
      s[0] = "userName"; 
      s[1] = "userPhone";
      int[] i = new int[2]; //用什么控件来装载上边String集合中的列 和上边的String数组也是一一对应的
      i[0] = android.R.id.text1;
      i[1] = android.R.id.text2;
      SimpleAdapter sim = new SimpleAdapter(this, listData, android.R.layout.simple_list_item_1, s, i); //这就是我们的篮子 
      userList.setAdapter(sim); //将篮子中的鸡蛋装到冰箱中 :)

  • 相关阅读:
    flash中网页跳转总结
    as3自定义事件
    mouseChildren启示
    flash拖动条移出flash无法拖动
    需要一个策略文件,但在加载此媒体时未设置checkPolicyFile标志
    Teach Yourself SQL in 10 Minutes
    电子书本地转换软件 Calibre
    Teach Yourself SQL in 10 Minutes
    Teach Yourself SQL in 10 Minutes
    Teach Yourself SQL in 10 Minutes – Page 31 练习
  • 原文地址:https://www.cnblogs.com/firecode/p/2700383.html
Copyright © 2011-2022 走看看