一、 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);

最新文章

  1. Buffer类
  2. AGS中通过FeatureServer插入数据失败、插入数据在WMTS请求中无法显示以及version概念的讨论
  3. python基础之运算符
  4. select接收后台返回值的解决方案
  5. Chrome浏览器之 Postman 安装
  6. SAP HR宏 rp-provide-from-last
  7. GDI学习之俄罗斯方块
  8. QQ Auto Login Visual Basic Script
  9. JS 和 Java 中URL特殊字符编码方式
  10. 结构类模式(二):桥接(Bridge)
  11. Android 换肤功能的实现(Apk插件方式)
  12. width:100% 和 max-width:100%; 有区别吗【转藏】
  13. 利用arpspoof和urlsnarf 进行ARP嗅探
  14. 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](十)
  15. 重启Oracle 服务
  16. 对于bilibili主页head部分的代码的总结以及疑问。
  17. Django建站+Vuejs前端
  18. all about
  19. Docker容器资源管理
  20. beego 各种形式的路由实例

热门文章

  1. Air Raid(最小路径覆盖)
  2. Oracle 11g新参数USE_LARGE_PAGES与AMM使用 (转载)
  3. Redis数据持久化之RDB持久化
  4. VC 获 取 当前程序运行路径的几种方法
  5. 下拉列表autocomplete各种实现方式比较
  6. 【HTTP】POST 与 PUT 方法区别
  7. 移动端拖拽(模块化开发,触摸事件,webpack)
  8. 慕课网-安卓工程师初养成-4-11 Java循环跳转语句之 break
  9. 【练习】显示MySQLadmin 库户籍选项
  10. Xcode去除某种类型的警告