警报对话框未出现在Android中

时间:2019-11-04 10:18:14

标签: android android-alertdialog

尝试设置待办事项的完成清单。我试图在单击按钮时使对话框显示,但该对话框没有出现。

showdone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(todo.this);
                builder.setAdapter(itemdone, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String strDone = itemdone.getItem(which);
                        AlertDialog.Builder builderInner = new AlertDialog.Builder(todo.this);
                        builderInner.setTitle("Done List");
                        builderInner.setMessage(strDone);
                        builderInner.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                        builderInner.create().show();
                    }
                });
                builder.show();
            }
        });

1 个答案:

答案 0 :(得分:1)

您要在对话框上而不是在构建器上调用show。也从builder重命名为showDoneBuilder,以与内部构建器区分开。拉出内部版本以提高可读性。

 showdone.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
          AlertDialog.Builder showDoneBuilder = new AlertDialog.Builder(todo.this);
            showDoneBuilder.setAdapter(itemdone, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String strDone = itemdone.getItem(which);
                    showDoneListDialog(strDone);
                }
            });
            showDoneBuilder.create().show(); // THE FIX
    }
 }

 private void showDoneListDialog(String message) {
       AlertDialog.Builder builderInner = new AlertDialog.Builder(todo.this);
       builderInner.setTitle("Done List");
       builderInner.setMessage(message);
       builderInner.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialog, int which) {
               dialog.dismiss();
           }
       });
       builderInner.create().show();
}