class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return blacknamebins.size();
}
@Override
public Object getItem(int i) {
//这个是返回当前选择条目内容
return blacknamebins.get(i);
}
@Override
public long getItemId(int i) {
//这个是返回当前选择条目的索引值
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
//返回一个条目所对应的View对象 (第三个参数为是否要挂在到父控件上 一般传入null)
View BlacknameView = View.inflate(Blackname_Activity.this, R.layout.listview_blackname, null);
//找到控件
tv_listView_blackname_phone = (TextView) BlacknameView.findViewById(R.id.tv_ListView_Blackname_Phone);
tv_listView_blackname_mode = (TextView) BlacknameView.findViewById(R.id.tv_ListView_Blackname_mode);
iv_blackname_del = (ImageView) BlacknameView.findViewById(R.id.iv_Blackname_del);
return BlacknameView;
}
}
优化:1:ConvertView的复用
2:findViewById()的复用
1:ConvertView的复用
if (view==null){
//返回一个条目所对应的View对象 (第三个参数为是否要挂在到父控件上 一般传入null)
view = View.inflate(Blackname_Activity.this, R.layout.listview_blackname, null);
}
2:findViewById()的复用
1:创建一个静态的ViewHolder类
里面创建你在ListView每个条目中想要找到的控件的名称
static class ViewHolder{
TextView tv_listView_blackname_phone;
TextView tv_listView_blackname_mode;
ImageView iv_blackname_del;
}
2:生成ViewHolder的对象
ViewHolder viewHolder = new ViewHolder();
3:如果是第一次
完整的
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = new ViewHolder();
if (convertView==null){
convertView = View.inflate(MainActivity.this, R.layout.item, null);
viewHolder.textView= (TextView)convertView.findViewById(R.id.te);
convertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textView.setText(object[position]);
return convertView;
}
}
static class ViewHolder{
TextView textView;
}