I am trying to open Dialog but I keep on getting compilation error of `Builder (android.content.Context) in Builder cannot be applied to the Activity

Ronnie :

The following code shows RecyclerView Class and underlying activity is Tab layout. Right now the way code is I am getting an error where I declare the AlertDialog the error says Builder cannot be applied to the Class if I use Context the app crashes. I have used the same code before but with Firestore Recycler Adapter and TabLayout. This question is not a duplicate, null pointer java/lang/nullpointerexception is too general. I have gone through all of them.

public class RecyclerAdapter extends FirestoreRecyclerAdapter<Items, 
RecyclerAdapter.MyViewHolder> {

Context context;


public RecyclerAdapter(@NonNull FirestoreRecyclerOptions<Items> options) 
{
    super(options);
}

@Override
protected void onBindViewHolder(@NonNull MyViewHolder holder, final int 
position, @NonNull final Items model) {
    holder.name.setText(model.getName());
    holder.category.setText(model.getCategory());
    holder.price.setText(model.getPrice());
    holder.manu.setText(model.getManufacturer());


    Picasso.get()
            .load(model.getImage())
            .fit()
            .centerCrop()
            .into(holder.Thumbnail);


    holder.addtocart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showAddToCartDialog(model);
        }
    });
}

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

class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView name;
    public TextView category;
    public ImageView Thumbnail;
    public TextView price;
    public TextView manu;
    public Button addtocart;

    public MyViewHolder(View itemView) {
        super(itemView);
        name = itemView.findViewById(R.id.rowname);
        category = itemView.findViewById(R.id.categorie);
        Thumbnail = itemView.findViewById(R.id.thumbnail);
        price = itemView.findViewById(R.id.price);
        manu = itemView.findViewById(R.id.manu);
        addtocart = itemView.findViewById(R.id.cartbutton);
    }
}

public void showAddToCartDialog(final Items items) {
    View view;

    AlertDialog.Builder builder = new 
    AlertDialog.Builder(RecyclerAdapter.this);
    LayoutInflater lflator = LayoutInflater.from(context);
    view = lflator.inflate(R.layout.add_to_cart_layout, null);

    ImageView image_product_dialogue = 
    view.findViewById(R.id.image_cart_product);
    TextView textView_product_dialogue = 
    view.findViewById(R.id.text_cart_product);
    RadioButton nairobi = view.findViewById(R.id.radio_nairobi);
    RadioButton mombasa = view.findViewById(R.id.radio_mombasa);
    RadioButton kisumu = view.findViewById(R.id.radio_kisumu);
    RadioButton nakuru = view.findViewById(R.id.radio_nakuru);
    RadioButton express = view.findViewById(R.id.radio_ex);
    RadioButton tomorow = view.findViewById(R.id.radio_tomo);
    final ElegantNumberButton text_count = 
    view.findViewById(R.id.text_count);

    express.setOnCheckedChangeListener(new 
    CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean 
        isChecked) {
            if (isChecked) {
                Items.delivery = 0;
            }
          }
      });

    tomorow.setOnCheckedChangeListener(new 
    CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean 
         isChecked) {
            if (isChecked)
                Items.delivery = 1;

        }
    });

    nairobi.setOnCheckedChangeListener(new 
    CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean 
   isChecked) {
            if (isChecked)
                Items.location = 0;

        }
    });
    mombasa.setOnCheckedChangeListener(new 
    CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean 
   isChecked) {
            if (isChecked)
                Items.location = 1;

        }
    });
    kisumu.setOnCheckedChangeListener(new 
    CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean 
  isChecked) {
            if (isChecked)
                Items.location = 2;

        }
    });
    nakuru.setOnCheckedChangeListener(new 
    CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean 
   isChecked) {
            if (isChecked)
                Items.location = 3;

        }
    });

    Picasso.get()
            .load(items.getImage())
            .fit()
            .centerCrop()
            .into(image_product_dialogue);
    textView_product_dialogue.setText(items.getName());

    builder.setView(view);

    builder.setNegativeButton("ADD TO CART", new 
    DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

            // showConfirmDialogue(position, text_count.getNumber(), 
   text_count.getNumber());
            dialog.dismiss();

        }
    });


    builder.show();

  }

 }
kelvin :

Pass activity context in the constructor and use that context to create Dialog. something like..

public RecyclerAdapter(@NonNull FirestoreRecyclerOptions<Items> options,Activity context) 
{
    super(options);
    this.context=context;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=327507&siteId=1