您的位置:首页 > 其它

安卓学习记录,BaseAdapter的使用

2016-03-02 23:18 232 查看
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: