java - 我正在尝试打开对话框,但我不断收到编译错误 `Builder (android.content.Context) in Builder cannot be applied to the Activity

标签 java android firebase

<分区>

以下代码显示了 RecyclerView 类,底层 Activity 是 Tab 布局。现在代码的方式是我在声明 AlertDialog 时遇到错误,错误说 Builder cannot be applied to the Class 如果我使用 Context 应用程序崩溃。我之前使用过相同的代码,但使用了 Firestore Recycler AdapterTabLayout。这道题不重复,null pointer java/lang/nullpointerexception太笼统了。我已经经历了所有这些。

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();

  }

 }

最佳答案

在构造函数中传递 Activity 上下文并使用该上下文创建对话框。像..

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

关于java - 我正在尝试打开对话框,但我不断收到编译错误 `Builder (android.content.Context) in Builder cannot be applied to the Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56288897/

相关文章:

android - Firebase Analytics 未显示 Android 应用的每日活跃用户

java - 基于体素的游戏中的 block 管理

java - Array.newInstance 上传递的泛型参数出现 nullpointerException

java - 如何简化 groovy 循环代码

java - 如何将泛型类型绑定(bind)到在层次结构中几乎完全分离的 2 种类型

javascript - 移至环境变量时 Firebase API key 未定义

java - 如何知道从android发送到服务器的数据包

java - 我正在尝试在 ArrayList 中放置一个标记

java - 在Android中,通过Intent传递子类的实例后,无法从父类(super class)方法中获取值

android - Firebase Android/iOS 缓存 Firestore 集合