I am trying to implement ViewHolder in my Android app, but I keep getting that ViewHolder cannot be resolved to a type, without any suggestions for an import. Anyone know ho
Maybe you are looking for the RecyclerView.ViewHolder which is part of the android support lib.
Like the code from this link gist by Paul Burke
public static class ItemViewHolder extends RecyclerView.ViewHolder implements
ItemTouchHelperViewHolder {
public final TextView textView;
public final ImageView handleView;
public ItemViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.text);
handleView = (ImageView) itemView.findViewById(R.id.handle);
}
@Override
public void onItemSelected() {
itemView.setBackgroundColor(Color.LTGRAY);
}
@Override
public void onItemClear() {
itemView.setBackgroundColor(0);
}
}
This would make sense for you if you were working with an Android RecyclerView
In this case they need an object to hold the view so that it can be filled with content as needed.
**Create a Holder class**
protected static class ViewHolderItems
{
private ImageView mStoreImage;
private TextView mStoreName;
}
And use In getView method of adapter
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderItems viewHolder;
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.coupen_row, null);
viewHolder = new ViewHolderItems();
viewHolder.mStoreImage = (ImageView) convertView.findViewById(R.id.storeImage);
viewHolder.mStoreName = (TextView) convertView.findViewById(R.id.storeName);
convertView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolderItems) convertView.getTag();
}
return convertView;
}
That's because a ViewHolder
is not a class that is from the Android SDK, you make it yourself.
Based on what I can find, a ViewHolder
is an implementation that stores Views (per row in a ListView usually) for a larger area, so it is a sort of helper class and cache mechanism. This is one example I found on Android Developers of what a ViewHolder
would contain.
static class ViewHolder {
TextView text;
TextView timestamp;
ImageView icon;
ProgressBar progress;
int position;
}
Then you can implement it in a ListAdapter
or a similar class.