0

I'm trying to set an onClickListener on one of my image on my recyclerview but the click doesn't work.

It looks like it doesn't seems to see there is a onClickListener, because when I set a "point" for the debugger on my listener it doesn't go to the point.

What I have done wrong ?

    public class Item_List_Adapter extends RecyclerView.Adapter<Item_List_Adapter.MyViewHolder> {
        // declare array
        private String[] mDataset;
        private Context mContext;

        public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
            public TextView itemName;
            public ImageView mAddBtn;

            public MyViewHolder(View v){
                super(v);

                itemName = (TextView) v.findViewById(R.id.item_name);
                mAddBtn = (ImageView) v.findViewById(R.id.action_add);
            }

            @Override
            public void onClick(View v) {
                System.out.println("TEST: ");
                switch (v.getId()) {
                    case R.id.action_add:
                        System.out.println("TEST2: ");
                        break;
                    default:
                        break;
                }
            }
        }

        // constructor
        public Item_List_Adapter(Context context, String[] myDataset) {
            mDataset = myDataset;
            mContext = context;
        }

        @Override
        public int getItemCount() {
            return mDataset.length;
        }

        @Override
        public Item_List_Adapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            // create a new view
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent, false);
            MyViewHolder nv = new MyViewHolder(v);
            return nv;
        }

        @Override
        public void onBindViewHolder(Item_List_Adapter.MyViewHolder holder, final int position) {
            holder.itemName.setText(mDataset[position]);

            holder.mAddBtn.setOnClickListener(holder);

        }
    }

and my XML file the one inflate in the recycler view

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="10dp"
    android:paddingBottom="10dp">

    <LinearLayout
        android:id="@+id/list_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginEnd="15dp"
        android:layout_marginStart="15dp">

        <ImageView
            android:id="@+id/action_check"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_action_check"
            android:alpha="0.3"
            />

        <TextView
            android:id="@+id/item_name"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight=".2"
            android:hint="Wine"
            android:alpha="0.3"
            android:gravity="start|center"
            android:paddingStart="5dp"
            android:paddingLeft="5dp"/>

        <ImageView
            android:id="@+id/action_remove"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_action_remove_green"
            android:onClick="item_remove"
            />
        <ImageView
            android:id="@+id/action_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_action_add_blue"
            />
        <ImageView
            android:id="@+id/action_chart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_action_chart"
            android:scaleType="centerInside"
            android:onClick="chart_detail"
            />

    </LinearLayout>

</LinearLayout>

Thanks in advance

Pierre
  • 675
  • 1
  • 8
  • 18
  • 4
    it is better to use it like this: `holder.mAddBtn.setOnClickListener(holder)` and implement `OnClickListener` interface in your `MyViewHolder` – pskink Oct 08 '16 at 08:17
  • What do you means ? Because If I write this I got: setOnclickListener(android.view.View.OnClickListener) in View cannot be applied to (com.app.pierre.myapp.adapter.Item_List_Adapter.MyViewHolder) – Pierre Oct 08 '16 at 08:25
  • try to add onclicklistener in your viewholder constructor ;) – Amir Ziarati Oct 08 '16 at 08:34
  • I have edit my question with your answer, so I have implement the onClickListener in MyViewHolder and override the onclick method, then in the onBindViewHodler I added the setOnClickListener.. but still doesn't work :( – Pierre Oct 08 '16 at 09:09
  • can you show the xml as well please – Matthew Shearer Oct 08 '16 at 09:13
  • Have you seen [RecyclerView onClick](http://stackoverflow.com/questions/24471109/recyclerview-onclick)? – OneCricketeer Oct 08 '16 at 09:15
  • Just tried and unfortunately it doesn't work, but I didn't know this ImageButton so thank for that.. I think something is blocking the OnclickListener but can't find what and where..because I can't get any Log anywhere.. I change all my System.out by Log. – Pierre Oct 08 '16 at 10:49

3 Answers3

0

you should use Log to output to the console

Log.d("TAG","TEST: );

for experimentation sake try this out in your ViewHolder constructor

mAddBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("TAG","TEST: );
        }
    });

If it doesn't work then something is blocking the click, this could be simply setting clickable="true" in the xml, or figuring out what is on front of the view

Matthew Shearer
  • 2,715
  • 3
  • 23
  • 32
0

The following is how I used in my Adapter. Hope this help:

    public class EventArrayAdapter extends RecyclerView.Adapter<ViewHolder> {
        private OnItemClickListener onItemClickListener;
        public OnItemClickListener getOnItemClickListener() {
            return onItemClickListener;
        }

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

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

        @Override
        public void onBindViewHolder(final ViewHolder holder, final int position) {
       //Add your viewHolder.button.setOnClickListener here as normal
        }

       @Override
       public ViewHolder onCreateViewHolder(ViewGroup parent, int arg1) {
            // TODO Auto-generated method stub
            View v = LayoutInflater.from(context).inflate(
                    R.layout.layout_item_event, parent, false);
            final EventViewHolder holder = new EventViewHolder(v);
            v.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    onItemClickListener.onItemClick(v, holder.getAdapterPosition());
                }
            });
            return holder;
        }
    }
Neo
  • 1,469
  • 3
  • 23
  • 40
0

update your constructor like this and try. It will work.

public MyViewHolder(View v){
            super(v);

            itemName = (TextView) v.findViewById(R.id.item_name);
            mAddBtn = (ImageView) v.findViewById(R.id.action_add);

            mAddBtn.setOnClickListener(this);    
        }

Basically assign onClick listener to your Imageview