kashifmehmood
kashifmehmood

Reputation: 127

progress bar not showing

hi guys check out the code below... i am using progress bar in my application but it is not showing when i use dialog.dismiss() but shows if i dont use this method but the problem then is that it does not go away.... any help guys...?

 AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("Are you sure you want to delete?")
                        .setCancelable(false)
                        .setPositiveButton("Yes",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int id) {
                                        `ProgressDialog` dialog1 = ProgressDialog.show(context, "", "Deleting...",true);
                                        // Log.v("", "You Clicked " + s);

                                        dba.delete("messages", "private = 0 and _id=?",
                                                new String[] { s });
                                        dba.close();
                                        populate();

                                    }
                                })
                        .setNegativeButton("No",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int id) {
                                        dialog.cancel();
                                    }
                                });

                AlertDialog alert = builder.create();
                alert.show();
                dialog1.dismiss();

Upvotes: 0

Views: 258

Answers (1)

Phix
Phix

Reputation: 9890

Few things I would clean up:

DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        switch(id){
        case DialogInterface.BUTTON_POSITIVE:

            ProgressDialog waitDialog = ProgressDialog.show(context, "", "Deleting...",true);
            dba.delete("messages", "private = 0 and _id=?", new String[] { s }) > 0);
            dba.close();

            waitDialog.dismiss();

            populate();

            break;
        case DialogInterface.BUTTON_NEGATIVE:
            dialog.dismiss();
            break;
        }
}

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to delete?")
       .setCancelable(false)
       .setPositiveButton("Yes", clickListener)
       .setNegativeButton("No", clickListener);

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

Its confusing to me why you'd would call alert.show() and immediately call dialog.dismiss(), when dialog won't be shown until you click on the POSITIVE button in the dialog.

Now, I can't remember at the moment if SQLite calls are blocking, by performing your delete operation, then closing the dialog right after, it should be done in the correct order. I haven't had my coffee yet ;)

Remember, though, that SQLite calls usually don't take too long, so for deleting one row from your database the ProgressDialog will flash on and flash off, if that. You might want to implement a Timer to have the progress show if the operation is taking longer than say, 300ms.

Also, by creating a new OnClickListener instead of using anonymous inner classes, things clear up and become more reusable.

This line: dialog.cancel(); Is interesting as well as you've set the dialog to be not cancellable. The difference between dismiss() and cancel() is that the cancel() call triggers the OnCancelListener, which you don't have set.

Upvotes: 1

Related Questions