NullPointerException
NullPointerException

Reputation: 37701

How to show a "are u sure YES OR NOT" dialog when user press a button?

first of all, i have to tell you that i have searched here and on google and i can't find a easy way to do it (im newbie on this), then i need your help please

i have this button, that removes a friend from the remote database:

removeButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                con.deletePermission(settings.getString("login",""),bundle.getString("email"));
                finish();
                Toast.makeText(getApplicationContext(), getString(R.string.friendsuccessfullyremoved), Toast.LENGTH_LONG).show();
            }
        }); 

i just want to show a simple "Are you sure? YES or NOT" dialog with two buttons (YES AND NOT) and when the user press YES, it have to be called this code:

con.deletePermission(settings.getString("login",""),bundle.getString("email"));
                    finish();
                    Toast.makeText(getApplicationContext(), getString(R.string.friendsuccessfullyremoved), Toast.LENGTH_LONG).show();

exist's a easy way to do it?

thanks

EDIT: i try to do it with this: http://developer.android.com/guide/topics/ui/dialogs.html but it doesn't works, nothing happens when i press the button, no dialog appear

my new code:

bundle = this.getIntent().getExtras();//get the intent & bundle passed by X
        builder = new AlertDialog.Builder(this);
        removeButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                /*
                con.deletePermission(settings.getString("login",""),bundle.getString("email"));
                finish();
                Toast.makeText(getApplicationContext(), getString(R.string.friendsuccessfullyremoved), Toast.LENGTH_LONG).show();
                */
                builder.setMessage("Are you sure?")
                       .setCancelable(false)
                       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                                con.deletePermission(settings.getString("login",""),bundle.getString("email"));
                                finish();
                                Toast.makeText(getApplicationContext(), getString(R.string.friendsuccessfullyremoved), Toast.LENGTH_LONG).show();
                           }
                       })
                       .setNegativeButton("No", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                           }
                       });
                AlertDialog alert = builder.create();
            }
        }); 

Upvotes: 1

Views: 912

Answers (2)

Jay Dee
Jay Dee

Reputation: 364

yeah it's missing

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

This is assuming you want to show the alert dialogue as soon as the button has been pressed.

Upvotes: 2

WarrenFaith
WarrenFaith

Reputation: 57702

From the documentation:

Creates a AlertDialog with the arguments supplied to this builder. It does not show() the dialog. This allows the user to do any extra processing before displaying the dialog. Use show() if you don't have any other processing to do and want this to be created and displayed.

That means, you need to call show().

Upvotes: 3

Related Questions