1

i'm new to Android development. I'm building an app that has cardviews inside a recyclerview. I want to make a single-choice alertdialog list and also handle them. I'm using FirebaseUI library to populate recyclerview.

Here is my ViewHolder class:

public class FriendListViewHolder extends RecyclerView.ViewHolder{

public CircleImageView friendPhoto;
public TextView friendName;
public CardView friendsCardView;

public FriendListViewHolder(@NonNull final View itemView) {
    super(itemView);

    friendPhoto = itemView.findViewById(R.id.friendProfilePhoto);
    friendName = itemView.findViewById(R.id.friendNameText);
    friendsCardView = itemView.findViewById(R.id.friends_cardview);

    }

}

Whenever user clicks on a cardview, alerdialog will pop-up. But keep in mind, i need to know which carview was selected.

Teyyihan Aksu
  • 116
  • 1
  • 8

2 Answers2

2

I'm trying it but not tested, and the concept is like this, let me know if not works for you

    public class FriendListViewHolder extends RecyclerView.ViewHolder{

    public CircleImageView friendPhoto;
    public TextView friendName;
    public CardView friendsCardView;

    public FriendListViewHolder(@NonNull final View itemView) {
        super(itemView);

        friendPhoto = itemView.findViewById(R.id.friendProfilePhoto);
        friendName = itemView.findViewById(R.id.friendNameText);
        friendsCardView = itemView.findViewById(R.id.friends_cardview);

        friendsCardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new AlertDialog.Builder(itemView.getContext())
                        .setTitle("Title of the alert dialog")
                        .setMessage("The body of the alert dialog")

                        // Specifying a listener allows you to take an action before dismissing the dialog.
                        // The dialog is automatically dismissed when a dialog button is clicked.
                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                // Continue with delete operation
                            }
                        })

                        // A null listener allows the button to dismiss the dialog and take no further action.
                        .setNegativeButton(android.R.string.no, null)
                        .setIcon(android.R.drawable.ic_dialog_alert)
                        .show();
            }
        });
    }

}

Although refer those questions RecyclerView onClick, How do I display an alert dialog on Android?

Jimale Abdi
  • 1,855
  • 1
  • 16
  • 27
0

In your adapter class override the onBindViewHolder method like the following

        @Override
        protected void onBindViewHolder(@NonNull FriendListViewHolder holder, final int position, @NonNull Model model) { 

            holder.friendsCardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new AlertDialog.Builder(context)
                        .setTitle("CardView Position")
                        .setMessage("CardView Position is " + String.valueOf(position))
                        .setCanceledOnTouchOutside(true)
                        .show();
                }
            });

        }

the above code will show an alert dialog containing the position of the cardView that has been clicked. I hope this will help you

Michael
  • 299
  • 2
  • 4
  • 14