一、 ArrayAdapter

  ListView listView = (ListView) findViewById(R.id.list_view);//ListView的参数为id

  listView.setAdapter(new ArrayAdapter<Person>(this, android.R.layout.simple_list_item_1, list));//对象是Person,第一个参数是context,第二个是指代要显示的模版,最后一个是要显示的数据,list为person类的ArrayList集合。

二、 BaseAdapter

  1、一行一行的显示对象

  ①、定义MyAdapter来继承BaseAdapter

   class MyAdapter extends BaseAdapter {

      @Override
      public int getCount() {
        return list.size();//list为person对象的List
      }

      @Override
      public Object getItem(int position) {
        return null;
      }

      @Override
      public long getItemId(int position) {
        return 0;
      }

      /**
      * 缓存的是被遮住的那一行
      */
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {

        TextView textview = null;

        if (null != convertView) {
        textview = (TextView) convertView;
        } else {
        textview = new TextView(MainActivity.this);
        }

        textview.setText(list.get(position).toString());

        return textview;
      }

   }

   ②、设置适配器

    ListView listview = (ListView) findViewById(R.id.list_view);

      listview.setAdapter(new MyAdapter());

  2、自定义一个xml,加入到ListView中再一行一行显示

    ①、定义自己的要显示的一行的内容布局文件----list_item.xml

    ②、定义MyAdapter来继承BaseAdapter    

    class MyAdapter extends BaseAdapter
    {

      @Override
      public int getCount() {
        return list.size();
      }

      @Override
      public Object getItem(int position) {
        return null;
      }

      @Override
      public long getItemId(int position) {
        return 0;
      }

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
        //布局转换器 作用就是讲一个布局转换为一个对象
        LayoutInflater flater = MainActivity1.this.getLayoutInflater();
        View view = flater.inflate(R.layout.list_item, null); //真正将一个布局文件转为一个对象
        //在一个特定的对象上去查找一个ID所对应的组件
        TextView text_name = (TextView) view.findViewById(R.id.list_view_name);
        TextView text_age = (TextView) view.findViewById(R.id.list_view_age);
        Person person = list.get(position);
        text_name.setText(person.getName());
        text_age.setText(String.valueOf(person.getAge()));
        return view;
      }
    }

  ③、设置适配器

     ListView listview = (ListView) findViewById(R.id.list_view);

     listview.setAdapter(new MyAdapter());

三、SimpleAdapter,显示的一行内容里面包含多行数据

  ①、定义自己的要显示的一行中要显示的多行的布局文件----list_item.xml

  ②、设置适配器(代码的意思是要显示的多行xml中是一行name,一行age);  

    ListView listview = (ListView) findViewById(R.id.list_view);

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

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

    data.add(info);

    info = new HashMap<String, String>();
    info.put("name", "wangwu");
    info.put("age", "111");
    data.add(info);

    info = new HashMap<String, String>();
    info.put("name", "wangwu");
    info.put("age", "111");
    data.add(info);

    info = new HashMap<String, String>();
    info.put("name", "wangwu");
    info.put("age", "111");
    data.add(info);

    info = new HashMap<String, String>();
    info.put("name", "wangwu");
    info.put("age", "111");
    data.add(info);

    info = new HashMap<String, String>();
    info.put("name", "wangwu");
    info.put("age", "111");
    data.add(info);

    SimpleAdapter simple = new SimpleAdapter(this, data,
    R.layout.list_item, new String[] { "name", "age" }, new int[] {
    R.id.list_view_name, R.id.list_view_age });

    listview.setAdapter(simple);

ListView的几种形式的更多相关文章

  1. 代替jquery $.post 跨域提交数据的N种形式

    跨域的N种形式: 1.直接用jquery中$.getJSON进行跨域提交 优点:有返回值,可直接跨域: 缺点:数据量小: 提交方式:仅get (无$.postJSON) $.getJSON(" ...

  2. C++:一般情况下,设计函数的形参只需要两种形式

    C++:一般情况下,设计函数的形参只需要两种形式.一,是引用形参,例如 void function (int &p_para):二,是常量引用形参,例如 void function(const ...

  3. jquery插件的两种形式

    这里总结一下jquery插件的两种形式,一种是通过字面量的形式组织代码,另一种是通过构造函数的方式.下面就两种形式来分析俩个例子. 例子1: ;(function ($,window,document ...

  4. javascript面向对象系列第三篇——实现继承的3种形式

    × 目录 [1]原型继承 [2]伪类继承 [3]组合继承 前面的话 学习如何创建对象是理解面向对象编程的第一步,第二步是理解继承.本文是javascript面向对象系列第三篇——实现继承的3种形式 [ ...

  5. 移动端App广告常见的10种形式

    什么是App广告?   App广告,或称In-App广告,是指智能手机和平板电脑这类移动设备中第三方应用程序内置广告,属于移动广告的子类别. App广告兴起得益于其载体—App的风行.平板电脑和大屏触 ...

  6. SQL 关于apply的两种形式cross apply 和 outer apply(转)

    转载链接:http://www.cnblogs.com/shuangnet/archive/2013/04/02/2995798.html apply有两种形式: cross apply 和 oute ...

  7. Struts2中Action接收参数的四种形式

    1.Struts2的Action接收参数的三种形式.      a. 使用Action的属性接收(直接在action中利用get方法来接收参数):                   login.js ...

  8. Node.js-提供了四种形式的定时器

    Node.js提供了四种形式的定时器 global.setTimeout(); //一次性定时器 global.setInterval(); //周期性定时器 global.nextTick(); / ...

  9. 参数传递的四种形式----- URL,超链接,js,form表单

    什么时候用GET,  查,删, 什么时候用POST,增,改  (特列:登陆用Post,因为不能让用户名和密码显示在URL上) 4种get传参方式 <html xmlns="http:/ ...

随机推荐

  1. Hibernate 实体关联关系映射【转】

    Hibernate关联关系映射目录│ ├─单向关联│  ├─  一对一外键单向关联│  ├─  一对一主键单向关联│  ├─  一对一连接表单向关联│  ├─  一对多外键单向关联│  ├─  一对多 ...

  2. WTL在Win8.1系统WM_DROPFILES无法响应的解决办法

    由于UAC的限制,WM_DROPFILES只能由权限较低的APP拖拽到权限较高的APP,反之如果从权限较高的APP拖拽到低权限的APP上,WM_DROPFILES不会被发送到低权限的APP消息队列.所 ...

  3. LPC1788 SDRAM运行程序

    折腾了很久 终于解决了 从SDRAM中运行APP程序. 说明:LPC1788 本身有512K的flash和96K的RAM.支持TFT和SDRAM 这算是跟别家cortex-M3架构MCU相比较的一个亮 ...

  4. C#中的委托和事件(续)

    转自张子阳的博客http://www.tracefact.net/CSharp-Programming/Delegates-and-Events-Advanced.aspx 引言 如果你看过了 C#中 ...

  5. Java - Nested Classes

    (本文参考:http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html) Nested Classes class OuterClas ...

  6. How to deploy JAVA Application on Azure Service Fabric

    At this moment, Azure Service Fabric does not support JAVA application natively (but it's on the sup ...

  7. visio2007无法拖动

    连按两下键盘上的 “Esc” 键

  8. 百度地图API的使用方法

    百度地图API 开始学习百度地图API最简单的方式是看一个简单的示例.以下代码创建了一个520x340大小的地图区域并以天安门作为地图的中心: 1. <html> 2. <head& ...

  9. bzoj1003 [ZJOI2006]物流运输

    1003: [ZJOI2006]物流运输 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 6300  Solved: 2597[Submit][Stat ...

  10. Devexpress 中如何写ASPxGridView新增修改时的数据验证

    //验证 protected void grid_Deptlist_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidatio ...