Which element that should be clicked when using adapter

64 views Asked by At

I have my activity_main.xml here :

enter image description here

I use adapter and recycler view to display each item. And this is my activity_list_item.xml:

enter image description here

I want to change/update my note so i need to add setOnClickListener method but i don't know which element that should be clicked. I tried to use setOnClickListener on recycler view but it did not work

1

There are 1 answers

0
Mochamad Taufik Hidayat On

in your adapter class you can put viewholder class that implement onClickListener

public class YourClassAdapter extends RecyclerView.Adapter<YourClassAdapter.YourViewHolder> {

    //your code

    class YourViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
       
        TextView price;
    
        public YourViewHolder(@NonNull View itemView) {
            super(itemView);
            //change to your view id
            price = itemView.findViewById(R.id.txtprecio1);
    
            itemView.setOnClickListener(this);
    
        }
    
    
      @Override
          public void onClick(View view) {
           //do something when clicked
       }
    }
}

then put your code you want on function onClick