0

I want to start an activity when i click on one of the item of my RecyclerView. Unfortunately when i click on an item nothing happens and I'm stuck here.

I think the code into the onClick method is right (But maybe I'm wrong). I tried to add a Log.d to see if the listener works but i can't see it in the logcat when i click on an item. I'm new on programming so it can be a stupid error. Thanks for help :)

public class UsersRecyclerAdapter extends RecyclerView.Adapter<UsersRecyclerAdapter.ViewHolder> {
private Context context;
private List<Users> usersList;
public UsersRecyclerAdapter(Context context, List<Users> usersList) {
    this.context = context;
    this.usersList = usersList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.users_row, parent, false);
    return new ViewHolder(view, context);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    final Users users = usersList.get(position);
    holder.firstName.setText(users.getFirstName());
    holder.lastName.setText(users.getLastName());
    holder.classe.setText(users.getClasse());
    String avatarURL = users.getAvatar();
    Picasso.get().load(avatarURL).into(holder.avatar);
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.i("MyApp","I am here");
            context = view.getContext();
            Intent intent = new Intent(context, PDFPostListActivity.class);
          //  String userID = users.getUserID();
          //  intent.putExtra("userID", userID);
            context.startActivity(intent);
        }
    });
}

EDIT. Here is the root view (users_row):

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imageViewAVATAR"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:paddingBottom="10dp"
        android:paddingLeft="10dp"
        android:paddingTop="10dp" />


    <TextView
        android:id="@+id/textViewfistName"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginEnd="10dp"
        android:gravity="center"
        android:textSize="21sp"
        android:textStyle="italic" />

    <TextView
        android:id="@+id/textViewlastName"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginEnd="10dp"
        android:gravity="center"
        android:textSize="21sp"
        android:textStyle="italic" />

    <TextView
        android:id="@+id/textViewclass"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="21sp"
        android:textStyle="italic" />

</LinearLayout>

</HorizontalScrollView>


 </androidx.cardview.widget.CardView>
  • Please [edit] your question to post the `users_row` layout. – Mike M. Aug 17 '19 at 14:47
  • Thank you for the advise. I added the line of code – Fabrizio Cacciapuoti Aug 18 '19 at 08:51
  • When you call `setOnClickListener()` on `itemView`, it's setting that on the ``, since that's the outermost `View` there. The `` nested within it is preventing it from receiving click events. Assuming that's all that going to be in that layout, then the simplest fix would probably be to simply set the `OnClickListener` on the `` instead. You'll need to give it an `android:id`, find it with `findViewById()` just like you did the `TextView`s, and then call `setOnClickListener()` on that, instead of `itemView`. – Mike M. Aug 18 '19 at 09:07
  • 1
    Oh man that's awesome. I didn't know there could be a similar problem. Thank you. I just try and now It works perfectly. – Fabrizio Cacciapuoti Aug 18 '19 at 09:16

2 Answers2

0

What's the root view of the view holder? Set it to be clickable since you set onClickListener on itemView of view holder.

Crowd
  • 21
  • 1
0

Great thanks to @Mike M. who rapidly answer in the comment below the question. I paste the message:

When you call setOnClickListener() on itemView, it's setting that on the , since that's the outermost View there. The nested within it is preventing it from receiving click events. Assuming that's all that going to be in that layout, then the simplest fix would probably be to simply set the OnClickListener on the instead. You'll need to give it an android:id, find it with findViewById() just like you did the TextViews, and then call setOnClickListener() on that, instead of itemView.