2
votes

I looked at several answers to this topic but none produced the desired result I want to have. I have an RecyclerView using the Firebase UI for RecyclerView where I want to add a OnClick event. So the user clicks on an item in the RecyclerView and gets to another activity. For this I am using an intent in my adapter class but somehow it is not working, if I click on an item of the RecyclerView nothings happens, it should open the next activity but it doesn't. Is there another way to open another activity?

Here is my adapter class:

class UserHolder extends RecyclerView.ViewHolder {

        TextView textViewUsername;

        public UserHolder(@NonNull View itemView) {
            super(itemView);

            textViewUsername = itemView.findViewById(R.id.player_name_search);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION && listener != null){
                        listener.onItemClick(getSnapshots().getSnapshot(position),(position));
                        Intent i = new Intent (itemView.getContext(), FriendRequest.class);
                        itemView.getContext().startActivity(i);
                    }

                }
            });
        }
    }

    public interface OnItemClickListener{
        void onItemClick (DocumentSnapshot documentSnapshot, int position);
    }

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

Here is the activity where I use the onItemClickListener:

userAdapter.setOnItemClickListener(new UserAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(DocumentSnapshot documentSnapshot, int position) {
                Intent i = new Intent(PlayerSearch.this, FriendRequest.class);
                startActivity(i);
            }
        });

Here is my list_item layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardElevation="5dp"
        app:cardCornerRadius="20dp"
        style="@style/CardViewWidgets"
        android:clickable="true"
        android:focusable="true">

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

            <ImageView
                android:id="@+id/picture_prize_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:scaleType="centerCrop"
                app:srcCompat="@mipmap/suezkanal"/>

            <TextView
                android:id="@+id/listitem_text_view_prize_game"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="@dimen/text_normal_size"
                android:text="Some text"
                android:layout_margin="8dp"
                style="@style/TextView"/>

        </LinearLayout>

    </androidx.cardview.widget.CardView>

</LinearLayout>
1
Please edit your question to explain exactly what "not working" means.Mike M.
If I click on an item in RecyclerView, it does not open the next activity, it does nothingKaiser
Is your activity declared in Manifest properly?Pratham Khurana
Yes both activities are declared in the manifest properlyKaiser
Remove the android:clickable and android:focusable attributes from the <CardView>. Or get rid of the outer LinearLayout, since it's rather pointless.Mike M.

1 Answers

0
votes

Thanks to the answer of @Mike M. this problem has been resolved. I had to delete my outer LinearLayout of the ListItem:

<androidx.cardview.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardElevation="5dp"
        app:cardCornerRadius="20dp"
        style="@style/CardViewWidgets"
        android:clickable="true"
        android:focusable="true"
        android:layout_margin="8dp">

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

            <ImageView
                android:id="@+id/picture_prize_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:scaleType="centerCrop"
                app:srcCompat="@mipmap/suezkanal"/>

            <TextView
                android:id="@+id/listitem_text_view_prize_game"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="@dimen/text_normal_size"
                android:text="Some text"
                android:layout_margin="8dp"
                style="@style/TextView"/>

        </LinearLayout>

    </androidx.cardview.widget.CardView>