Skip to content
Advertisement

Recycler view inside full screen alert dialog does not showing items

I have a recycler view inside my full screen alert dialog, but for some reason it doesn’t display any of the items inside the ArrayList passed to the adapter.

I tried:

  • Initializing the adapter after adding the items inside the ArrayList
  • Use notifyItemInserted instead of using notifyDataSetChanged
  • Checking if there is a problem with the item layout

My method that creates the full screen dialog:

private void showCommentsDialog() {

        AlertDialog dialog = new AlertDialog.Builder(context, R.style.Theme_SocialMeme).create();
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View dialogView = layoutInflater.inflate(R.layout.comments_dialog_fragment, null);

        ImageButton dismissDialogButton = dialogView.findViewById(R.id.imageButton17);
        EditText comment = dialogView.findViewById(R.id.writeCommentET);
        ImageButton addCommentBtn = dialogView.findViewById(R.id.imageButton18);
        RecyclerView commentsRecyclerView = dialogView.findViewById(R.id.comments_recycler_view);

        ArrayList<CommentModel> commentModelArrayList = new ArrayList<>();
        CommentsRecyclerAdapter adapter = new CommentsRecyclerAdapter(commentModelArrayList, context, dialog.getOwnerActivity());
        commentsRecyclerView.setAdapter(adapter);

        DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
        rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                if (snapshot.child("posts").child(postID).hasChild("comments")) {
                    for (DataSnapshot commentsSnapshot : snapshot.child("posts").child(postID).child("comments").getChildren()) {
                        CommentModel commentModel = new CommentModel();
                        commentModel.setAuthor(commentsSnapshot.child("author").getValue(String.class));
                        commentModel.setCommentID(commentsSnapshot.child("commentID").getValue(String.class));
                        commentModel.setAuthorUsername(commentsSnapshot.child("authorUsername").getValue(String.class));
                        commentModel.setPostID(commentsSnapshot.child("postID").getValue(String.class));
                        commentModel.setAuthorProfilePictureURL(commentsSnapshot.child("authorProfilePictureURL").getValue(String.class));
                        commentModel.setCommentText(commentsSnapshot.child("commentText").getValue(String.class));
                        commentModelArrayList.add(commentModel);
                        adapter.notifyItemInserted(commentModelArrayList.size() -1);
                    }
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Toast.makeText(context, "Error: " + error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

        dismissDialogButton.setOnClickListener(view -> dialog.dismiss());
        
        dialog.setView(dialogView);
        dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
        dialog.show();

    }

My Adapter:

public class CommentsRecyclerAdapter extends RecyclerView.Adapter {

    List<CommentModel> commentsList;
    Context context;
    Activity activity;

    public CommentsRecyclerAdapter(List<CommentModel> commentsList, Context context, Activity activity) {
        this.commentsList = commentsList;
        this.context = context;
        this.activity = activity;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.comment_item, parent, false);
        return new CommentViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        CommentViewHolder viewHolder = (CommentViewHolder) holder;

        // Load profile picture
        String profilePictureUrl = commentsList.get(position).getAuthorProfilePictureURL();
        if (profilePictureUrl != null) {
            if (!profilePictureUrl.equals("none")) {
                Glide.with(context).load(profilePictureUrl).into(viewHolder.profilePicture);
            }
        }

        viewHolder.usernameTV.setText(commentsList.get(position).getAuthorUsername());
        viewHolder.commentContent.setText(commentsList.get(position).getCommentText());

    }

    @Override
    public int getItemCount() {
        return commentsList.size();
    }
}

My Dialog view:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
    android:background="?attr/background"
    tools:context=".Activities.NotificationsActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar4"
            android:layout_width="0dp"
            android:layout_height="70dp"
            android:background="@color/white"
            android:minHeight="?attr/actionBarSize"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView24"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-black"
            android:text="Comments"
            android:textColor="@color/black"
            android:textSize="40sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="@+id/toolbar4"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.081"
            app:layout_constraintStart_toStartOf="@+id/toolbar4"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.411" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/comments_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dp"
            app:layout_constraintTop_toBottomOf="@+id/view19"
            tools:itemCount="10"
            tools:listitem="@layout/comment_item" />

        <androidx.cardview.widget.CardView
            android:id="@+id/view19"
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:layout_marginBottom="12dp"
            android:layout_marginStart="12dp"
            android:layout_marginEnd="12dp"
            app:cardCornerRadius="12dp"
            app:cardElevation="8dp"
            app:layout_constraintBottom_toTopOf="@+id/comments_recycler_view"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/toolbar4">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <de.hdodenhof.circleimageview.CircleImageView
                    android:id="@+id/comments_profile_image"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:src="@drawable/user"
                    app:civ_border_color="#FF000000"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.045"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <EditText
                    android:id="@+id/writeCommentET"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:ems="10"
                    android:hint="Write a comment"
                    android:inputType="textPersonName"
                    app:layout_constraintBottom_toBottomOf="@+id/comments_profile_image"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.0"
                    app:layout_constraintStart_toEndOf="@+id/comments_profile_image"
                    app:layout_constraintTop_toTopOf="@+id/comments_profile_image" />

                <ImageButton
                    android:id="@+id/imageButton18"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="16dp"
                    android:background="@color/white"
                    app:layout_constraintBottom_toBottomOf="@+id/writeCommentET"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="1.0"
                    app:layout_constraintStart_toEndOf="@+id/writeCommentET"
                    app:layout_constraintTop_toTopOf="@+id/writeCommentET"
                    app:srcCompat="@drawable/ic_arrow_forward" />

            </androidx.constraintlayout.widget.ConstraintLayout>


        </androidx.cardview.widget.CardView>

        <ImageButton
            android:id="@+id/imageButton17"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_marginEnd="8dp"
            android:background="@color/white"
            android:foreground="?selectableItemBackgroundBorderless"
            app:layout_constraintBottom_toBottomOf="@+id/toolbar4"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@+id/toolbar4"
            app:srcCompat="@drawable/ic_close_black" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

My item view layout:

<?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"
    app:cardElevation="8dp"
    android:layout_marginStart="12dp"
    android:layout_marginEnd="12dp"
    android:layout_marginTop="15dp"
    app:cardCornerRadius="12dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#6FFFFFFF"
        android:padding="15dp">

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/circleImageView3"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@drawable/user"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/textView65"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:text="Username"
            android:textSize="20sp"
            android:textStyle="bold"
            android:textColor="?attr/textFillColor"
            app:layout_constraintBottom_toBottomOf="@+id/circleImageView3"
            app:layout_constraintStart_toEndOf="@+id/circleImageView3"
            app:layout_constraintTop_toTopOf="@+id/circleImageView3" />

        <TextView
            android:id="@+id/textView69"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="Comment text"
            android:textStyle="bold"
            android:textSize="18sp"
            android:textColor="?attr/titleTextColor"
            app:layout_constraintStart_toStartOf="@+id/circleImageView3"
            app:layout_constraintTop_toBottomOf="@+id/circleImageView3" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>

Advertisement

Answer

You have forgot to add LayoutManager to Recyclerview. If you are listing it vertically you can add layout manager as the following:

In View:

app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:orientation="vertical"

In Code:

commentsRecyclerView.setLayoutManager(new LinearLayoutManager(context, RecyclerView.VERTICAL, false));
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement