I'm trying to wrap my head around getView (), and I don't think there is any theme in Android that creates more confusion and questions in StackOverflow and elsewhere. Everyone wants to know why he is called so many times or in what order or not, but, as Romain Guy says, here , "there is absolutely no guarantee on the order in which getView () will be called and how many times.
So, I have another question : I do not understand the convertView parameter .
I have a list of 15 items, 11 of which can fit on the screen. The first time the application starts, getView () is called 48 times.
convertView has a null value for position 0 on the first call, a non-zero value for positions 1-11, then non-zero for position 0 and null for positions 1-11, then null for position 0 and non-zero for positions 1-11 and finally, not zero for positions 0-11.
Can anyone explain why / when the convertView is null or non-null, how / why it starts a non-zero value for most positions and why the same positions seem to go back and forth between these two states?
References to good tutorials written in plain English that explain convertView in detail will also be appreciated.
PS - my tests were conducted on a device running Android 2.3.5, if that matters. I know that Google has changed ListActivity / adapter / getView several times since then.
( ). , : " ?" ,
protected class PLxxxAdapter extends BaseAdapter {
public PLxxxAdapter(Context c) {
}
@Override
public int getCount() {
return listItems.size();
}
@Override
public Object getItem(int position) {
return listItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
boolean select;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.PLxxxitem, null);
convertView.setLayoutParams(new ListView.LayoutParams(
ListView.LayoutParams.MATCH_PARENT,
ListView.LayoutParams.MATCH_PARENT));
holder = new ViewHolder();
holder.itemView = (TextView)convertView.findViewById(R.id.post);
holder.cV1 = (CheckedTextView)convertView.findViewById(R.id.check1);
holder.cV1.setCompoundDrawablesWithIntrinsicBounds(0, R.layout.smallcb, 0, 0);
holder.cV1.setOnClickListener(new ConfBoxListener());
convertView.setTag(holder);
holder.cV1.setTag(holder);
}
else {
holder = (ViewHolder)convertView.getTag();
}
try {
int liSize = listItems.size();
if (position < liSize) {
holder.itemView.setText(listItems.get(position));
}
}
catch (Exception e) {
Log.e ("PLxxxActivity.getView Crash", "details " + e);
}
holder.cV1.setChecked(confirmed.get(position));
select = selected.get(position);
if (select == true) {
convertView.setBackgroundResource(R.color.colBlue);
}
else
convertView.setBackgroundResource(R.color.colGrey);
holder.position = position;
if (RemoteControlActivity.confCBs == true)
holder.cV1.setVisibility(View.VISIBLE);
else
holder.cV1.setVisibility(View.INVISIBLE);
return convertView;
}
}