zoukankan      html  css  js  c++  java
  • listview的两种适配器

    一、 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);
  • 相关阅读:
    leetcode 350. Intersection of Two Arrays II
    leetcode 278. First Bad Version
    leetcode 34. Find First and Last Position of Element in Sorted Array
    leetcode 54. Spiral Matrix
    leetcode 59. Spiral Matrix II
    leetcode 44. Wildcard Matching
    leetcode 10. Regular Expression Matching(正则表达式匹配)
    leetcode 174. Dungeon Game (地下城游戏)
    leetcode 36. Valid Sudoku
    Angular Elements
  • 原文地址:https://www.cnblogs.com/labixiaoxin/p/4933746.html
Copyright © 2011-2022 走看看