[android] How to dismiss AlertDialog in android

I think there's a simpler solution: Just use the DialogInterface argument that is passed to the onClick method.

AlertDialog.Builder db = new AlertDialog.Builder(context);
        db.setNegativeButton("cancel", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface d, int arg1) {
                db.cancel();
                //here db.cancel will dismiss the builder

            };  
        });

See, for example, http://www.mkyong.com/android/android-alert-dialog-example.