title: Unable to show AlertDialog in recycler view adapter class
body: <p>I have button in recycler view rows and I want to show alert dialog on button click.I am setting <code>onClickListener</code> in <code>onBindViewHolder</code> method of adapter class but when I click on button it is showing run time error given below:</p>
  
  <pre><code>&gt; 2019-12-25 22:22:57.140 11041-11041/com.app.aamku E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.app.aamku, PID: 11041
  android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
      at android.view.ViewRootImpl.setView(ViewRootImpl.java:765)
      at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
      at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
      at android.app.Dialog.show(Dialog.java:330)
      at Adapters.ProductAdapter$2.onClick(ProductAdapter.java:174)
      at android.view.View.performClick(View.java:6294)
      at android.view.View$PerformClick.run(View.java:24770)
      at android.os.Handler.handleCallback(Handler.java:790)
      at android.os.Handler.dispatchMessage(Handler.java:99)
      at android.os.Looper.loop(Looper.java:164)
      at android.app.ActivityThread.main(ActivityThread.java:6494)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
  </code></pre>
  
  <p>Below is my code:</p>
  
  <pre><code>public class ProductAdapter extends RecyclerView.Adapter&lt;ProductAdapter.ViewHolder&gt; {
  
  private Context context;
  private List&lt;ProductsModel&gt; productList;
  
  public ProductAdapter(Context context, List&lt;ProductsModel&gt; productList) {
      this.context = context;
      this.productList = productList;
  }
  
  @NonNull
  @Override
  public ProductAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  
      View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.selectpack_layout,parent,false);
  
      ViewHolder viewHolder = new ViewHolder(v);
  
      return viewHolder;
  }
  
  @Override
  public void onBindViewHolder(@NonNull final ProductAdapter.ViewHolder holder, final int position) {
  
      final ProductsModel model = productList.get(position);
  
      holder.marketName.setText(model.getMarketName());
  
      holder.order.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
  
              AlertDialog.Builder builder = new AlertDialog.Builder(context);
              builder.setMessage("Confirm order");
              builder.setCancelable(true);
              builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialogInterface, int i) {
  
                      dialogInterface.cancel();
                  }
              });
              builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialogInterface, int i) {
  
                     Toast.makeText(context,"Hello",Toast.Length_SHORT).show();
  
                  }
              });
  
              AlertDialog alertDialog = builder.create();
              alertDialog.show();
  
          }
      });
  }
  
  @Override
  public int getItemCount() {
      return productList.size();
  }
  
  public class ViewHolder extends RecyclerView.ViewHolder{
  
      Button order;
      TextView marketName;
  
      public ViewHolder(@NonNull View itemView) {
          super(itemView);
  
          order = itemView.findViewById(R.id.order);
          marketName = itemView.findViewById(R.id.marketName);
  
       }
     }
  }
  </code></pre>
  
  <p>How can I overcome this issue someone please let me know what I am doing wrong any help would be appreciated.</p>
  
  <p><strong>THANKS</strong></p>