1

I have the following RecyclerViewAdapter class. What's weird about the following code is if I don't declare the ViewHolder class static, I get the following error:

Error:(24, 8) error: MaterialRequiredListRecyclerViewAdapter is not abstract and does not override abstract method onBindViewHolder(MaterialRequiredListRecyclerViewAdapter.MaterialRequiredRecyclerViewHolder,int) in Adapter

However as soon as I declare this class static, the error goes away.

I know that declaring the ViewHolder class enforces the prevention of memory leaks by not allowing the inner class use the member variables of the outer class or expose it's own instance to other classes, however I don't understand the reason for the above mentioned error.

Code:

public class MaterialRequiredListRecyclerViewAdapter<T> extends RecyclerView.Adapter<MaterialRequiredListRecyclerViewAdapter.MaterialRequiredRecyclerViewHolder> {

    private int mLayoutFileId;
    private List<String> mMaterialRequiredDataList;
    private Context mContext;

    public MaterialRequiredListRecyclerViewAdapter(Context con, int layoutFileId, List materialRequiredList) {
        mContext = con;
        mLayoutFileId = layoutFileId;
        mMaterialRequiredDataList = materialRequiredList;
    }

    @Override
    public MaterialRequiredRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.
                from(parent.getContext()).
                inflate(mLayoutFileId, parent, false);//R.layout.layout_cardview_apointments_for_video_call_frag

        return new MaterialRequiredRecyclerViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MaterialRequiredRecyclerViewHolder holder, int position) {
        if(mMaterialRequiredDataList != null)
            holder.mMaterialRequiredTV.setText(mMaterialRequiredDataList.get(position));
    }

    @Override
    public int getItemCount() {
        return mMaterialRequiredDataList.size();
    }

    public class MaterialRequiredRecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        @BindView(R.id.text_view_list_cell)
        TextView mMaterialRequiredTV;

        public MaterialRequiredRecyclerViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        public void onClick(View v) {

        }
    }
}
user2498079
  • 2,872
  • 8
  • 32
  • 60
  • Read this question - http://stackoverflow.com/questions/13516835/why-in-viewholder-pattern-should-the-viewholder-class-be-static – Arjit May 15 '17 at 06:08
  • I have read the question, what it fails to answer is if we are declaring the viewholder class as static to avoid creation of instance for each row in the recyclerView, that's not gonna work because for certain number of rows, the view holder will be instantiated and none of those objects of the viewholder are sharing data/states between them. so what's the point of making the viewholder static? – user2498079 May 15 '17 at 06:23

2 Answers2

1

If your question is just this:

I don't understand the reason for the above mentioned error.

Then the answer is that making the view holder class static makes it accessible from anywhere, whereas making it non-static makes it inaccessible with just its name reference like MaterialRequiredRecyclerViewHolder holder. If you want to keep it non-static, just change the following lines in your adapter:

@Override
public void onBindViewHolder(MaterialRequiredRecyclerViewHolder holder, int position) {
    if(mMaterialRequiredDataList != null)
        holder.mMaterialRequiredTV.setText(mMaterialRequiredDataList.get(position));
}

to this:

@Override
public void onBindViewHolder(MaterialRequiredListRecyclerViewAdapter.MaterialRequiredRecyclerViewHolder holder, int position) {
    if(mMaterialRequiredDataList != null)
        holder.mMaterialRequiredTV.setText(mMaterialRequiredDataList.get(position));
}

The call to the non-static inner class should be through a reference to the outer class. I'm sorry for my bad English, but I hope I could explain.

Belladonna
  • 99
  • 1
  • 8
  • Why won't it work with method signature like `public void onBindViewHolder(MaterialRequiredRecyclerViewHolder holder, int position)` if I don't declare the viewholder static? – user2498079 May 15 '17 at 06:49
  • @user2498079 like I said, the `onBindViewHolder` method is unable to access the inner class because it is not static and therefore not visible in that scope. Did you try it this way? – Belladonna May 15 '17 at 07:11
0

You have to make all methods in view holder class public. It has to be accessed in the adapter class. The error goes if u make it public.

Bensal
  • 3,316
  • 1
  • 23
  • 35