简体   繁体   English

单击按钮时不显示AlertDialog

[英]AlertDialog not displaying when button clicked

I have created an AlertDialog to appear with information displayed from my DB. 我创建了一个AlertDialog来显示数据库中显示的信息。 The issue is the AlertDialog box does not show at all. 问题是AlertDialog框根本不显示。 There are no errors which occur which makes it difficult to solve the problem. 没有发生错误,因此很难解决该问题。

 public void viewAll() {
    button.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Cursor result = myDB.getData();
                    if (result.getCount() == 0) {
                        showMessage("ERROR", "NO DATA");
                        return;
                    }
                    StringBuffer buffer = new StringBuffer();
                    while (result.moveToNext()) {
                        buffer.append("COL1: " + result.getString(1) + "\n");
                        buffer.append("COL2: " + restul.getString(2) + "\n");
                        buffer.append("COL3: " + result.getString(3) + "\n");
                        buffer.append("COL4: " + result.getString(4) + "\n");

                    }

                    showMessage("DATA", buffer.toString());
                }
            }
    );
}
public void showMessage(String title, String message) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(message);
    builder.show();
}

Forgive me for using pseudo code 原谅我使用伪代码

Create AlertDialog first then call show on that AlertDialog object. 首先创建AlertDialog然后在该AlertDialog对象上调用show。 Like this: 像这样:

AlertDialog dialog = builder.create();
dialog.show();

Try this instead: 尝试以下方法:

AlertDialog.Builder adb = new AlertDialog.Builder(this)
.setCancelable(true)
.setTitle(title)
.setMessage(message)
.create();
adb.show();

or use my code and replace .create() with .show(). 或使用我的代码并将.create()替换为.show()。

public void showMessage(String title, String message) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(message);
    builder.create().show();
}

use this. 用这个。 hope it will work. 希望它能工作。 if not you can change the line like: 如果没有,您可以像这样更改行:

AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this);

then it should work. 那么它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM