Android基础--ListView的显示几种方式

目前常用的ListView显示的方式有三种

【1】BaseAdapter

【2】ArrayAdapter

【3】SimpleAdapter

通过API Code 可以看出,实际上ArrayAdapter,SimpleAdater都是继承了BaseAdapter.所以这里主要讨论BaseAdapter里面的方法。

BaseAdapter的方法主要有四个

【1.1】getCount() //当前数据的总条数

【1.2】getItem(int position) //获取某一项

【1.3】getItemId(int position) //获取某一项的ID

【1.4】getView(int position,View convertView,ViewGroup parent)//生成一个View的视图


若要实现ListView的显示,必须实现 getCount和getView 的方法


首先基于BaseAdapter来显示ListView


主页面布局



    
    

ListView 实际上是有一项一项的数据组成的,那么就需要抽象出来一个item

Item的布局如下




    

    


MainActivity


    private ListView listView;
    private DBHelper dbHelper;
    private List personList;

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

        dbHelper=new DBHelper(getApplicationContext());
        listView=(ListView) findViewById(R.id.lv);
        personList=new ArrayList<>();
    }

  

    //显示ArrayAdapter
    public void showArrayAdapter(View view){
        Intent intent=new Intent(getApplicationContext(),ArrayAdapterActivity.class);
        startActivity(intent);
    }

    //显示SimpleAdapter
    public void showSimpleAdapter(View view){
        Intent intent=new Intent(getApplicationContext(),SimpleAdapterActivity.class);
        startActivity(intent);
    }

    public void selectList(View view){
        SQLiteDatabase db=dbHelper.getReadableDatabase();
        Cursor cursor= db.query("person",null,null,null,null,null,null);
         if(cursor!=null&&cursor.getCount()>0){
            while (cursor.moveToNext()){
                String name=cursor.getString(1);
                String phone=cursor.getString(2);

                Person person=new Person();
                person.setName(name);
                person.setPhone(phone);
                personList.add(person);
            }
            listView.setAdapter(new MyAdapter());
        }

    }

    private class MyAdapter extends BaseAdapter{

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

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

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

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            View newView;
            if(view==null){
                newView=View.inflate(getApplicationContext(),R.layout.item,null); //打气筒
            }else{
                newView=view;
            }
            TextView tv_name=newView.findViewById(R.id.name);
            TextView tv_phone=newView.findViewById(R.id.phone);
            Person person=personList.get(i);
            tv_name.setText(person.getName());
            tv_phone.setText(person.getPhone());

            return newView;
        }
    }


【1.2】ArrayAdapter的实现方式

ArrayAdapter 选择构造函数的时候,需要注意你当前显示数据的MODEL,选择构造函数是带MODEL,否则是无法执行getView()方法


布局如下:




    

    


实现方式

    private List personList;
    private DBHelper dbHelper;
    private ListView lv;

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


        lv=(ListView)findViewById(R.id.array_lv);
        dbHelper=new DBHelper(getApplicationContext());
        personList=getPersonList();

        if(personList.size()>0){
            MyArrayAdapter myArrayAdapter=new MyArrayAdapter(getApplicationContext(),R.layout.item,personList);
            lv.setAdapter(myArrayAdapter);
        }else{
            Toast.makeText(getApplicationContext(),"显示数据失败",Toast.LENGTH_LONG).show();
        }
    }

    private class MyArrayAdapter extends ArrayAdapter {
        //选择构造函数的时候,如果是带类型的,就一定要将MODEL,放入到构造函数中
        public MyArrayAdapter(Context context, int resource, List objects) {
            super(context, resource, objects);
        }

        //重写方法
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view;
            if(convertView==null){
                view=View.inflate(getApplicationContext(),R.layout.item,null);//打气筒
            }else{
                view=convertView;
            }
            TextView tv_name=view.findViewById(R.id.name);
            TextView tv_phone=view.findViewById(R.id.phone);
            Person person=personList.get(position);
            tv_name.setText(person.getName());
            tv_phone.setText(person.getPhone());
            return view;
        }
    }



    private List getPersonList(){
        List list=new ArrayList<>();
        SQLiteDatabase db=dbHelper.getReadableDatabase();
        Cursor cursor= db.query("person",null,null,null,null,null,null);
        if(cursor!=null&&cursor.getCount()>0){
            while (cursor.moveToNext()){
                String name=cursor.getString(1);
                String phone=cursor.getString(2);
                Person person=new Person();
                person.setName(name);
                person.setPhone(phone);
                list.add(person);
            }
        }
        return list;
    }


【1.3】SimpleAdapter的显示方式比较简单,布局方式与ArrayAdapter是一致的,所以就直接上是实现方式了

    private DBHelper dbHelper;
    private ListView lv;

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

        lv=(ListView) findViewById(R.id.simple_lv);
        dbHelper=new DBHelper(getApplicationContext());
        SimpleAdapter simpleAdapter=new SimpleAdapter(getApplicationContext(),getPersonList(),R.layout.item,
              new String[]{"name","phone"},new int[]{R.id.name,R.id.phone});

        lv.setAdapter(simpleAdapter);
    }

    private List> getPersonList(){
        List> mapList=new ArrayList<>();
        SQLiteDatabase db=dbHelper.getReadableDatabase();
        Cursor cursor= db.query("person",null,null,null,null,null,null);
        if(cursor!=null&&cursor.getCount()>0){
            while (cursor.moveToNext()){
                Map map=new HashMap<>();
                String name=cursor.getString(1);
                String phone=cursor.getString(2);

                map.put("name",name);
                map.put("phone",phone);

                mapList.add(map);
            }
        }
        return mapList;
    }

注意的要点:

[1]这个几个适配器实际上都是为了将数据展现在ListView上,都是需要获取每一项数据,来拼接成当前页面的数据。

而,页面手机的可视范围是有限,所以,构造显示的数据的时候。可以将getView()里面的item重复利用。

就是代码中提到的“打气筒"

[2]使用什么适配器,就构造成需要的MODEL.可以直接重写getView()里面的方式,实现具体的业务



你可能感兴趣的:(Android基本学习)