简体   繁体   中英

AlertDialog not displaying when button clicked

I have created an AlertDialog to appear with information displayed from my DB. The issue is the AlertDialog box does not show at all. 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. 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().

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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