androidandroid-recyclerviewonitemclicklistener

Cannot resolve method setOnItemClickListener for RecyclerView


I am working with a RecyclerView and I am trying to set OnClick's and OnLongClick's for each of the item's in the RecyclerView. I apologize but I don't think I truly understand what is going on with the code, some of it was taken off slides for my assignment and some patches for stuff that wouldn't work that I found on this site. At the top of my RecyclerView Adapter class I hav static OnItemClickListener mItemClickListener;` And then I have an interface for my new methods, a method to set my clicklistener, and then my ViewHolder implementation like so:

public interface OnItemClickListener {
        void onItemClick(View view, int position);
        void onItemLongClick(View view, int position);
    }

    public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView movieImage;
        public TextView movieName;
        public TextView movieDescription;
        public CheckBox movieCheckBox;
        public CardView cV;

        public ViewHolder(View view) {
            super(view);

            movieImage = (ImageView) view.findViewById(R.id.movieImg);
            movieName = (TextView) view.findViewById(R.id.movieName);
            movieDescription = (TextView) view.findViewById(R.id.movieDescription);
            movieCheckBox = (CheckBox) view.findViewById(R.id.movieCheckBox);

            view.setOnClickListener(new View.OnClickListener(){
               @Override
                public void onClick(View v) {
                   if (mItemClickListener != null) {
                       if (getAdapterPosition() != RecyclerView.NO_POSITION) {
                           mItemClickListener.onItemClick(v, getAdapterPosition());
                       }
                   }
               }
            });

            view.setOnLongClickListener(new View.OnLongClickListener() {
               @Override
                public boolean onLongClick(View v) {
                   if (mItemClickListener != null) {
                       if (getAdapterPosition() != RecyclerView.NO_POSITION) {
                           mItemClickListener.onItemLongClick(v, getAdapterPosition());
                       }
                   }
                   return true;
               }
            });
        }
    }

My issue comes in the class for my fragment which owns the recycler view, where I am trying to set the onitemclicklistener.

myAdapter = new MyRecyclerAdapter();

        myAdapter.setOnItemClickListener(new MyRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Log.d("TEST", "onItemClick");
            }

            @Override
            public void onItemLongClick(View view, int position) {
                Log.d("TEST", "onItemLongClick");
            }
        });

It says the issue is with myAdapter.setOnItemClickListener it says

'Cannot resolve method 'setOnItemClickListener(anonymous com.example.kevin.assignment4.MyRecyclerAdapter.OnItemClickListener)'
But I don't know why it's saying that. It should be able to find the method just fine as far as I can tell.

EDIT: My imports for MyRecyclerView class are as follows:

import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.HashMap;

Solution

  • So I'm not sure why my original method didn't work, since I've seen similar methods online and even one of the answers posted here worked the same way. I solved my issue by changing the structure of my adapter class so that it takes the listener in as a parameter to the constructor of the RecyclerView adapter. There, I set the listener in the class equal to the listener that came in.

    public MyRecyclerAdapter(ClickListener clickListener) {
            setClickListener(clickListener);
        }
    

    And then in my fragment where I create the recyclerview I have

    myAdapter = new MyRecyclerAdapter(this);

    My fragment class implements the interface from my adapter class.