我在ListView中有一个ContextMenu,它可以选择从ListView中删除一个项目。 因此,当用户从ContextMenu中选择DELETE时,我想显示一个AlertDialog,要求确认此删除。
但是当在onMenuItemSelected(..)中调用AlertDialog.show()时,它不会显示Dialog。此对话框仅在onMenuItemSeletect(..)
的“return true”后显示 @Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
//0: for OPEN of R.array.context_menu_categories
case 0:
break;
//2: DELETE
case 2:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
// Setting Dialog Title
alertDialog.setTitle("Delete...");
// Setting Dialog Message
alertDialog.setMessage("Do you want to delete this?");
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User pressed YES button. Write Logic Here
Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User pressed No button. Write Logic Here
Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
}
});
alertDialog.show();
break;
default:
break;
}
return true;
}
答案 0 :(得分:0)
这就是Android的工作原理。在应用程序重绘自身之前,图形元素不会出现,这发生在Android事件循环中。要返回事件循环,您必须退出Android调用的函数。在此函数返回之前无法显示它。