Android - AlertDialog within onContextItemSelected does not work
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() :)
精彩评论