gpt4 book ai didi

java - 警报 Dialog.Builder(??)

转载 作者:行者123 更新时间:2023-11-29 06:49:47 25 4
gpt4 key购买 nike

我正在尝试为用户按下卡片 View 以将其删除时制作一个警报对话框。这是在我的 ProductAdapter 代码中

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

我会在括号中放什么????我查看了该站点上的示例,我看到的示例在括号内使用了 ActivityName.this。但是,此代码位于 ProductAdapter 代码中,我无法使用 ProductAdapter.this。 (我的主要 Activity 称为 create.java fyi)

那里面会放什么呢?谢谢

更新

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {

private Map<Integer, Integer> mSpinnerSelectedItem = new HashMap<Integer, Integer>();





//this context we will use to inflate the layout
private Context mCtx;
private SearchableSpinner spinner;

//we are storing all the products in a list
private List<Product> productList;

private Activity create;

public ProductAdapter(Activity activity){
create = activity;
}






//getting the context and product list with constructor
public ProductAdapter(Activity activity, List<Product> productList) {
this.mCtx = mCtx;
create = activity;
this.productList = productList;
}

@Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//inflating and returning our view holder
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.layout_products, null);
return new ProductViewHolder(view);
}

@Override
public void onBindViewHolder(ProductViewHolder holder, final int position) {
// //getting the product of the specified position


ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(mCtx, R.layout.item_spinner_layout,
Product.getSpinnerItemsList());
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

holder.spinner.setAdapter(spinnerArrayAdapter);

holder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int mPosition, long id) {
mSpinnerSelectedItem.put(position, mPosition);

TextView mTextView = view.findViewById(R.id.mSpinnerText);
Toast.makeText(mCtx, "Selected Item: " + mTextView.getText().toString(), Toast.LENGTH_LONG).show();
Log.e("***************", "Selected Item: " + mTextView.getText().toString());


}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});


//binding the data with the viewholder views
if (mSpinnerSelectedItem.containsKey(position)) {
holder.spinner.setSelection(mSpinnerSelectedItem.get(position));
}


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


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


// set title
alertDialogBuilder.setTitle("Delete Item");

// set dialog message
alertDialogBuilder
.setMessage("Are you sure you want to delete this item?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity

productList.remove(position);
notifyItemRemoved(position);



}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});

// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();

// show it
alertDialog.show();









}
});
}






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





class ProductViewHolder extends RecyclerView.ViewHolder {

SearchableSpinner spinner;
EditText editText;
TextView textView5;
CheckBox checkBox;
LinearLayout linearLayout;
View rootView;




public ProductViewHolder(View itemView) {
super(itemView);

spinner = itemView.findViewById(R.id.spinner);
editText = itemView.findViewById(R.id.editText);
textView5 = itemView.findViewById(R.id.textView5);
checkBox = itemView.findViewById(R.id.checkBox);
rootView = itemView.findViewById(R.id.linearLayout);

}

public View getView() {
return rootView;
}

}





}

最佳答案

尽管 AlertDialog.Builder() 在其参数中接受一个 Context 引用,但最好传递一个 Activity 引用给它,而不是applicationContext 因为潜在的主题和窗口附件问题。因此,将 Activity 引用传递给您的适配器并使用它来构建 AlertDialog

public class ProductAdapter extends SomeAdapter {

private Activity create;

public ProductAdapter(Activity activity, List<Product> productList) {
create = activity;
this.productList = productList;
}

private void showAlertDialog(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(create);
// ...
alertDialogBuilder.create().show();
}

}

关于java - 警报 Dialog.Builder(??),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52575746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com