开发者

Android - AlertDialog within onContextItemSelected does not work

开发者 https://www.devze.com 2023-01-26 11:31 出处:网络
I have a following pseudo-code. public boolean onContextItemSelected(MenuItem aItem) { switch(aItem.getItemId()) {

I have a following pseudo-code.

public boolean onContextItemSelected(MenuItem aItem) {
      switch(aItem.getItemId()) {
           case A: {
                new AlertDialog.Builder(this)
                  .setIcon(android.R.drawable.ic_dialog_alert)
                  .setTitle("Delete")
                  .setMessage("Delete?")
        开发者_开发技巧          .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                       @Override
                       public void onClick(DialogInterface dialog, int which) {
                             // do stuff A...
                       }
                  });

                  // do stuff B...

                  return true;
           }
      }
  }

The problem is that it never shows the alert dialog. However, it does things as stated in "do stuff B..."

Does anyone know why AlertDialog is now showing?

Thank you!


you need to .create() .show() will solve the problem :)

AlertDialog dialog = new AlertDialog.Builder(this)
              .setIcon(android.R.drawable.ic_dialog_alert)
              .setTitle("Delete")
              .setMessage("Delete?")
              .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int which) {
                         dialog.dismiss();
                   }
              }).create();
dialog.show();

And when you don't need it anymore you can dismiss() it.

Edit: sorry. forgot .create() :)

0

精彩评论

暂无评论...
验证码 换一张
取 消