Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView.Adapter does not see overridden onBindViewHolder method?

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?

like image 930
Tiberal Avatar asked Oct 08 '15 14:10

Tiberal


2 Answers

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.

like image 56
user1978019 Avatar answered Oct 13 '22 00:10

user1978019


in this line:

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    super.onBindViewHolder(holder, position);
}

replace ViewHolder with BaseAdapterExtended.ViewHolder

like image 38
Shayan_Aryan Avatar answered Oct 13 '22 00:10

Shayan_Aryan