Trinity
Trinity

Reputation: 484

How to get context from on click method in Dialogbox

I'm trying to develop a simple Sqlite application to understand Crud operations. I have book class user should be able to select instert update and delete.When user wants to delete a data, user is asked that he is want to delete book data.There is two buttons on alertdialog box Yes or No. If user click on setPositiveButton deletion operation is materialized. But i can't get context in Code onClick Method.Code is as following

 AlertDialog.Builder alertDialog = new AlertDialog.Builder(KitapDetay.this);
                alertDialog.setTitle("Alert");
                alertDialog.setMessage("Do you want to delete book ?");
                alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        BookRepo repo = new BookRepo(this);
                        repo.deleteBook(id);
                        Toast.makeText(getApplicationContext(), "Book is deleted successfully", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                        startActivity(intent);
                        finish();
                    }
                });

Upvotes: 0

Views: 2286

Answers (2)

jtt
jtt

Reputation: 13541

Because you're in a Anonymous object defintion, you can't actually reference methods that would be otherwise applicable to the Activity. In your case if you want to get Context from within your onClick, then you can do it either one of two ways:

1) Store the Context into a global variable in the onCreate of your Activity so that it can be used by your OnClick

2) Do a explicit reference to your Activity like this in your OnClick:

<ActivityName>.this

Upvotes: 3

Androider
Androider

Reputation: 3873

Try this, it works:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);

            alertDialogBuilder.setTitle("Your Title");

            alertDialogBuilder
                .setMessage("Click yes to exit!")
                .setCancelable(false)
                .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // if this button is clicked, close
                        // current activity
                        MainActivity.this.finish();
                    }
                  })
                .setNegativeButton("No",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {

                        dialog.cancel();
                    }
                });

                // create an alert dialog object
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show alert dialog here
                alertDialog.show();
  • Use context of your activity like "MainActivity.this".

Upvotes: 0

Related Questions