Ilnar Karimov
Ilnar Karimov

Reputation: 339

Adapter getView is called multiple times

My getView is called multiple times. I set fill_parent width and fill_parent height of listview, but it doesn't work. See the code of adapter:

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) {
//  }
  }

My Log:

called times: 0

called times: 1

called times: 2

called times: 0


My adapter is drawing wrong number of listview items and it's problem

enter image description here

Upvotes: 0

Views: 1677

Answers (1)

Nazgul
Nazgul

Reputation: 1902

Of course it will be. The getView method in adapter is called for each row in the List that is visible on screen so it will obviously be called multiple times.

Upvotes: 2

Related Questions