What is the utility of ViewHolder
in a custom Adapter
?
I have created an Adapter
using the ViewHolder
and an other without and I have the same result... so what is the utility of this ?
This is my CustomAdapter.class with the ViewHolder
:
public class CustomAdapter extends ArrayAdapter<Fruit> {
public class ViewHolder {
TextView txtName;
TextView txtColor;
}
CustomAdapter(Context context, ArrayList<Fruit> data) {
super(context, R.layout.item_fruit, data);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
final Fruit fruit = getItem(position);
ViewHolder viewHolder = new ViewHolder();
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.item_fruit, parent, false);
}
viewHolder.txtName = (TextView) convertView.findViewById(R.id.textView_fruit_name);
viewHolder.txtColor = (TextView) convertView.findViewById(R.id.textView_fruit_color);
if (fruit != null) {
viewHolder.txtName.setText(fruit.getName());
viewHolder.txtColor.setText(fruit.getColor());
}
return convertView;
}
}
This is my CustomAdapter.class without the ViewHolder
:
public class CustomAdapter extends ArrayAdapter<Fruit> {
CustomAdapter(Context context, ArrayList<Fruit> data) {
super(context, R.layout.item_fruit, data);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
final Fruit fruit = getItem(position);
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.item_fruit, parent, false);
}
TextView txtName = (TextView) convertView.findViewById(R.id.textView_fruit_name);
TextView txtColor = (TextView) convertView.findViewById(R.id.textView_fruit_color);
if (fruit != null) {
txtName.setText(fruit.getName());
txtColor.setText(fruit.getColor());
}
return convertView;
}
}
[Link](http://stackoverflow.com/questions/21501316/what-is-the-benefit-of-viewholder) – Sandeep dhiman May 03 '17 at 08:45