When you are developing an Android program; and you want to have a ArrayAdapter you can Simply have a Class (most of times with ViewHolder suffix) or directly inflate your convertView and find your view by id.
So What is the benefit of using ViewHolder?
The example of both here :
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = ((Activity)getContext()).getLayoutInflater().inflate(R.layout.row_phrase, null);
}
((TextView) convertView.findViewById(R.id.txtPhrase)).setText("Phrase 01");
}
Or create an inner class in the ArrayAdapter as following:
static class ViewHolder {
ImageView leftIcon;
TextView upperLabel;
TextView lowerLabel;
}
and finally in the getView :
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.row_layout,
null, false);
holder = new ViewHolder();
holder.leftIcon = (ImageView) view.findViewById(R.id.leftIcon);
}
}
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…