Mapping AlertDialog to ViewHolder in RecyclerView adapter class

I have implemented RecyclerView with some rows, and now I am trying to use AlertDialog to display a message when the user clicks on a row.

I successfully implemented setOnClickListener in the adapter, but I cannot get AlertDialog to work, the system keeps telling me that I cannot use AlertDialog.Builder in ViewHolder:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

My github code is here

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public TextView textView;

    public ViewHolder(View itemView) {
        super(itemView);
        textView = (TextView) itemView;
        itemView.setOnClickListener(this);
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setTitle(null);
        {
            alertDialogBuilder
                    .setMessage("You selected ")
                    .setCancelable(true); // True allows you to use the back button to exit the dialog, false does not
        }
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }
+4
source share
2 answers

AlertDialog alertDialog = new AlertDialog.Builder(itemView.getParent().getContext());

.

-1

Source: https://habr.com/ru/post/1613843/


All Articles