0

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

3 Answers3

4

Create AlertDialog first then call show on that AlertDialog object. Like this:

AlertDialog dialog = builder.create();
dialog.show();
Talha Mir
  • 1,228
  • 11
  • 19
0

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().

0
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.

takmilul
  • 96
  • 6