I use DataBindings and RecyclerView in my project. I have a base adapter for RecyclerView. It looks like this
public abstract class BaseAdapter<T extends ViewDataBinding> extends RecyclerView.Adapter<BaseAdapter.ViewHolder> {
public BaseAdapter() {}
public class ViewHolder extends RecyclerView.ViewHolder {
public T binding;
public ViewHolder(View view) {
super(view);
binding = DataBindingUtil.bind(view);
}
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
... code ...
}
}
ViewHolder extended classes differs only binding field type. After the implementation of the extended BaseAdapter class:
public class BaseAdapterExtended extends BaseAdapter<BaseAdapterExtendedBinding> {
public BaseAdapterExtended(ArrayList<ItemModel> itemModels) {
super();
mData = itemModels;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
return new ViewHolder(BaseAdapterExtendedBinding.inflate(inflater, parent, false).getRoot());
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
super.onBindViewHolder(holder, position);
}
@Override
protected View getItemRootView(ViewHolder holder) {
return holder.binding.item;
}
@Override
public int getItemCount() {
return mData.size();
}
}
i am got next compilation error:
error: BaseAdapterExtended is not abstract and does not override abstract method onBindViewHolder(BaseAdapter.ViewHolder,int) in Adapter
Seems like BaseAdapterExtended hasn't this method, but he exists. If I change
public void onBindViewHolder(ViewHolder holder, int position)
to
public void onBindViewHolder(BaseAdapter.ViewHolder holder, int position)
Projections compiled fine, but type binding will be ViewDataBinding instead BaseAdapterExtendedBinding. Why is this happening? Any ideas?
In my case I had forgotten to parameterize my superclass. When creating the class I hadn't yet created the ViewHolder
, and it was very permissive except for the error on that method. I.e.:
public class FooAdapter extends RecyclerView.Adapter {
Had to be changed to
public class FooAdapter extends RecyclerView.Adapter<FooAdapter.FooViewHolder> {
This needed to be done anyways, but the only compiler error I got was in onBindViewHolder
.
in this line:
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
super.onBindViewHolder(holder, position);
}
replace ViewHolder with BaseAdapterExtended.ViewHolder
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With