문제

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() :)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top