Grand Skunk
Grand Skunk

Reputation: 39

Alert Dialog.Builder(??)

I am trying to make an alertdialog for when the user presses the cardview to delete it. This is in my ProductAdapter code

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

what would I put in the brackets where I have the ???? I have looked at examples on this site and the ones I have seen have used ActivityName.this inside the brackets. However this code is in the ProductAdapter code and I can't use ProductAdapter.this. (my main activity is called create.java fyi)

So what would go in there? Thanks

update

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;
        }

    }





}

Upvotes: 2

Views: 1044

Answers (3)

aminography
aminography

Reputation: 22832

Although AlertDialog.Builder() accepts a Context reference in its argument, it is better to pass an Activity reference to it, not applicationContext because of potential theme and window attachment problems. So, pass an Activity reference to your adapter and use it to build an 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();
    }

}

Upvotes: 3

Neeraj Bagra
Neeraj Bagra

Reputation: 144

Only pass the context of calling activity to your ProductAdapter class and use this

Upvotes: 2

Kinjal Rathod
Kinjal Rathod

Reputation: 549

You must pass context of ther activity there, check this link for more understanding.

Upvotes: 2

Related Questions