安卓学习记录,BaseAdapter的使用

来源:互联网 发布:一梦似瑶台,心知玉女来 编辑:程序博客网 时间:2024/05/29 17:09

PersonBean类:

package com.example.ln.baseadapterdemo;/** * Created by Ln on 2016/3/1. */public class PersonBean {    private String name;    private String desc;    public PersonBean() {    }    public PersonBean(String name, String desc) {        this.name = name;        this.desc = desc;    }    public String getName() {        return name;    }    public String getDesc() {        return desc;    }    public void setName(String name) {        this.name = name;    }    public void setDesc(String desc) {        this.desc = desc;    }    @Override    public String toString() {        return "PersonBean{" +                "name='" + name + '\'' +                ", desc='" + desc + '\'' +                '}';    }}

MyAdapter类:

package com.example.ln.baseadapterdemo;import android.content.Context;import android.text.Layout;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.public class MyAdapter extends BaseAdapter {    private LayoutInflater inflater;    private List<PersonBean> list;    public MyAdapter(List<PersonBean> personlist, Context context) {        list = personlist;        inflater = LayoutInflater.from(context);    }    @Override    public int getCount() {        return list.size();    }    @Override    public Object getItem(int position) {        Log.i("test", "" + position);        return null;    }    @Override    public long getItemId(int position) {        return 0;    }    @Override    public View getView(int position, View convertView, ViewGroup parent) {//        //方式一://        View view = inflater.inflate(R.layout.item, null);//        TextView tv_name = (TextView) view.findViewById(R.id.item_tv_name);//        TextView tv_desc = (TextView) view.findViewById(R.id.item_tv_desc);////        PersonBean personBean = list.get(position);//        tv_name.setText(personBean.getName());//        tv_desc.setText(personBean.getDesc());//        return view;        //方式二:        if (convertView == null) {            convertView = inflater.inflate(R.layout.item, null);        }        TextView tv_name = (TextView) convertView.findViewById(R.id.item_tv_name);        TextView tv_desc = (TextView) convertView.findViewById(R.id.item_tv_desc);        PersonBean personBean = list.get(position);        tv_name.setText(personBean.getName());        tv_desc.setText(personBean.getDesc());        return convertView;//        //第三种 方式:////        Viewholder viewholder = null;//        if (convertView == null) {//            viewholder = new Viewholder();//            convertView = inflater.inflate(R.layout.item,null);//            viewholder.tv_name = (TextView) convertView.findViewById(R.id.item_tv_name);//            viewholder.tv_desc = (TextView) convertView.findViewById(R.id.item_tv_desc);//            convertView.setTag(viewholder);//        }//        PersonBean personBean = list.get(position);//        viewholder = (Viewholder) convertView.getTag();//        viewholder.tv_name.setText(personBean.getName());//        viewholder.tv_desc.setText(personBean.getDesc());//        return convertView;    }    public class Viewholder{        private TextView tv_name;        private TextView tv_desc;    }}
0 0
原创粉丝点击