2

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

    }





}
aminography
  • 21,986
  • 13
  • 70
  • 74
Grand Skunk
  • 29
  • 18

3 Answers3

3

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

}
aminography
  • 21,986
  • 13
  • 70
  • 74
  • it says that the activity is not an enclosing class? – Grand Skunk Sep 30 '18 at 07:52
  • see this: https://stackoverflow.com/questions/5436822/why-does-alertdialog-buildercontext-context-only-accepts-activity-as-a-paramet – aminography Sep 30 '18 at 07:54
  • What do you mean about enclosing class? – aminography Sep 30 '18 at 07:57
  • thts what it says when I hover over `create.this` – Grand Skunk Sep 30 '18 at 08:00
  • 1
    It's not necessary to add ".this" for "create". Because it is an Activity instance and Activity inherits fom Context class which means it is already a Context instance. – aminography Sep 30 '18 at 08:05
  • i changed that. but I get this error https://pastebin.com/WNaEBPK2 on this line `AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(create);` – Grand Skunk Sep 30 '18 at 08:11
  • I guess you used "public ProductAdapter(Context mCtx, List productList)" constructor that does not initialize the "create" Activity reference. Please change this constructor to get an activity reference like your first constructor. – aminography Sep 30 '18 at 08:15
  • how would I do that? – Grand Skunk Sep 30 '18 at 08:19
  • See constructor changes in above answer. – aminography Sep 30 '18 at 08:20
  • hmm I that thanks but I still get the error https://pastebin.com/4yMBB9dg btw line 62 is `LayoutInflater inflater = LayoutInflater.from(mCtx);` and line 26 is `public class ProductAdapter extends RecyclerView.Adapter {` – Grand Skunk Sep 30 '18 at 08:27
  • 1
    mCtx is not initialized and is null. Please remove "Context mCtx" from ProductAdapter class and use "create" everywhere you need a context reference. – aminography Sep 30 '18 at 08:30
2

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

Kinjal Rathod
  • 499
  • 4
  • 12
2

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

Neeraj Bagra
  • 134
  • 7