Harsh Lakhani
Harsh Lakhani

Reputation: 125

Context in alertdialog

Here is the code for alert dialog I am using:

new AlertDialog.Builder(AlertDemo.this)
    .setTitle("This is Alert Demo")
    .setMessage("Here is an Alert Message!")
    .setNeutralButton("Close", new DialogInterface.OnClickListener() 
    {
        public void onClick(DialogInterface dlg, int sumthin) 
        {
            // do nothing – it will close on its own
        }
    })
    .show();    

when i write this instead of AlertDemo.this it shows an error The constructor AlertDialog.Builder(new View.OnClickListener(){}) is undefined.. What does it mean ? what is the difference between this and AlertDemo.this ?

Upvotes: 1

Views: 489

Answers (1)

Mohit Verma
Mohit Verma

Reputation: 3025

If you simply use this, it is an instance of inner class View.onClickListener

In order to show alertdialog on click of a button you need to pass the instance of you AlertDemo class.

That's why you use AlertDemo.this

Upvotes: 2

Related Questions