适配器getView被多次调用

时间:2014-12-09 15:05:46

标签: android listview

我的getView被多次调用。我设置listview的fill_parent width和fill_parent高度,但它不起作用。 请参阅适配器代码:

public class CustomAdapter extends BaseAdapter implements OnCheckedChangeListener {
private Context ctx;
public ArrayList<Objects> listToSend;
private ViewHolder holder;
private TextView tv;
private LayoutInflater mInflater;


public AdapterOrderType(Context context, ArrayList<Objects> list) {
    this.ctx = context;
    this.listToSend = list;
    mInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

@Override
public Object getItem(int position) {
    return listToSend.get(position);
}

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

static class ViewHolder {
    private TextView tv;
    private LinearLayout ll;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    Object object = objects.get(position);

     ViewHolder holder = null;
     if (convertView == null) {
         convertView = mInflater.inflate(R.layout.item_objects, null);
         holder = new ViewHolder();

        holder.tv = (TextView)convertView.findViewById(R.id.objname);

         convertView.setTag(holder);
     } else {
         holder = (ViewHolder)convertView.getTag();
     }
    Log.e("called times: ",String.valueOf(position)); 
    return convertView;
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//  }
  }

我的日志:

呼叫时间:0

呼叫时间:1

呼叫时间:2

呼叫时间:0


我的适配器正在绘制错误数量的listview项目,这是问题

enter image description here

1 个答案:

答案 0 :(得分:2)

当然会。适配器中的getView方法为List中显示在屏幕上的每一行调用,因此显然会多次调用它。