首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >通知Dialog.Builder(??)

通知Dialog.Builder(??)
EN

Stack Overflow用户
提问于 2018-09-30 07:40:48
回答 3查看 796关注 0票数 2

我正在尝试做一个警告对话框,当用户按下卡视图删除它。这在我的ProductAdapter代码中

代码语言:javascript
运行
复制
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(????);

我应该在括号里放什么?我看过这个站点上的例子,以及我在括号中看到的使用ActivityName.this的例子。但是,这段代码在ProductAdapter代码中,我不能使用ProductAdapter.this。(我的主要活动叫create.java fyi)

那里面会有什么呢?谢谢

更新

代码语言:javascript
运行
复制
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;
        }

    }





}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-09-30 07:46:31

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

代码语言:javascript
运行
复制
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();
    }

}
票数 3
EN

Stack Overflow用户

发布于 2018-09-30 07:45:02

您必须在那里传递活动的上下文,请查看链接以获得更多的理解。

票数 2
EN

Stack Overflow用户

发布于 2018-09-30 07:51:48

只将调用活动的上下文传递给您的ProductAdapter类并使用

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52575746

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档