android ListView简单学习

ListView#### MVC架构

  • M: 模型层 --- 集合

  • V: 视图层 --- ListView

  • C:控制层 --- Adapter

Adapter

  • ListView的每一个条目都是一个View对象
BaseAdapter

public class MainActivity extends Activity {

    List persons = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        persons = new ArrayList();
        Person person;
        for (int i = 0; i < 200; i++) {
            String id = i + "";
            String name = "张三" + i;
            String phone = 158741 + i + "";
            String salary = (16000 + i * 100) + "";
            person = new Person(id, name, phone, salary);
            persons.add(person);
        }

        ListView lv = (ListView) findViewById(R.id.lv);
        lv.setAdapter(new MyAdapter());
    }

    class MyAdapter extends BaseAdapter {

        // 系统调用,用来获知集合中有多少个元素
        @Override
        public int getCount() {
            return persons.size();
        }

        // 由系统调用,获取一个View对象,作为listView的条目
        // position: 本次getView方法调用所返回的View对象,在ListView中是处于第几个条目,那么position的值就是多少
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // 第一种方式
            /*
             * TextView tv = new TextView(MainActivity.this); Person person =
             * persons.get(position); tv.setText(person.toString());
             * tv.setTextSize(18); return tv;
             */

            // 第二种方式
            // 把布局文件填充为一个View对象
            View v = View.inflate(MainActivity.this, R.layout.item_listview,
                    null);
            /*LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
            View v2 = inflater.inflate(R.layout.item_listview, null);

            LayoutInflater inflater2 = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            View v3 = inflater2.inflate(R.layout.item_listview, null);*/
            
            Person person = persons.get(position);
            TextView tvName = (TextView) v.findViewById(R.id.tv_name);
            tvName.setText(person.getName());
            TextView tvPhone = (TextView) v.findViewById(R.id.tv_phone);
            tvPhone.setText(person.getPhone());
            TextView tvSalary = (TextView) v.findViewById(R.id.tv_salary);
            tvSalary.setText("¥" + person.getSalary());
            return v;
        }

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

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

listView优化


public class MainActivity extends Activity {

    List persons = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        persons = new ArrayList();
        Person person;
        for (int i = 0; i < 200; i++) {
            String id = i + "";
            String name = "张三" + i;
            String phone = 158741 + i + "";
            String salary = (16000 + i * 100) + "";
            person = new Person(id, name, phone, salary);
            persons.add(person);
        }

        ListView lv = (ListView) findViewById(R.id.lv);
        lv.setAdapter(new MyAdapter());
    }

    class MyAdapter extends BaseAdapter {

        // 系统调用,用来获知集合中有多少个元素
        @Override
        public int getCount() {
            return persons.size();
        }

        // 由系统调用,获取一个View对象,作为listView的条目
        // position: 本次getView方法调用所返回的View对象,在ListView中是处于第几个条目,那么position的值就是多少
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // 把布局文件填充为一个View对象
            View v = null;
            //判断条目是否有缓存
            if (convertView == null) {
                v = View.inflate(MainActivity.this, R.layout.item_listview,
                        null);
            } else {
                v = convertView;
            }

            Person person = persons.get(position);
            TextView tvName = (TextView) v.findViewById(R.id.tv_name);
            tvName.setText(person.getName());
            TextView tvPhone = (TextView) v.findViewById(R.id.tv_phone);
            tvPhone.setText(person.getPhone());
            TextView tvSalary = (TextView) v.findViewById(R.id.tv_salary);
            tvSalary.setText("¥" + person.getSalary());
            return v;
        }

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

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

填充View的三种方式


// 把布局文件填充为一个View对象
View v = View.inflate(MainActivity.this, R.layout.item_listview, null);
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View v2 = inflater.inflate(R.layout.item_listview, null);

LayoutInflater inflater2 = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View v3 = inflater2.inflate(R.layout.item_listview, null);  

ArrayAdapter 只适合处理一个变量

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        ListView listView = (ListView) findViewById(R.id.lv);
        
        String[] objects = new String[]{
            "小志",
            "大志",
            "中志"
        };
        listView.setAdapter(new ArrayAdapter(this, R.layout.item_listview, R.id.tv_name, objects));
    }
}

效果图:

[图片上传中。。。(1)]

SimpleAdapter 简单的适配器,比BaseAdapter简单,比ArrayAdapter复杂一些,可以处理多个变量
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView listView = (ListView) findViewById(R.id.lv);

        String[] objects = new String[] { "小志", "大志", "中志" };
        
        //集合中每个元素都包含ListView条目需要的所有数据,该案例中每个条目需要一个字符串和一个整型,所有使用一个map来封装这两种数据
        List> data = new ArrayList>();
        Map map1 = new HashMap();
        map1.put("photo", R.drawable.phone1);
        map1.put("name", "小志");
        data.add(map1);
        
        Map map2 = new HashMap();
        map2.put("photo", R.drawable.phone2);
        map2.put("name", "中志");
        data.add(map2);
        
        Map map3 = new HashMap();
        map3.put("photo", R.drawable.phone3);
        map3.put("name", "大志");
        data.add(map3);
        
        listView.setAdapter(new SimpleAdapter(this, data, R.layout.item_listview, new String[]{
                "photo", "name"
        }, new int[]{
                R.id.iv_photo, R.id.tv_name
        }));
    }
}  

你可能感兴趣的:(android ListView简单学习)