Skip to content Skip to sidebar Skip to footer

How Can I Access My Activity's Instance Variables From Within An AlertDialog's OnClickListener?

Here's a very simplified version of my Activity: public class Search extends Activity { //I need to access this. public SearchResultsAdapter objAdapter; public boolea

Solution 1:

Using Search.this.objAdapter to access objAdapter from the listener should work.

Search.this refers to the current instance of Search and allow you to access its fields and methods.


Solution 2:

Make your activity implement OnClickListener:

public class Search  extends Activity implements DialogInterface.OnClickListener { ...

Add the onclick method to your activity:

public void onClick(DialogInterface dialog, int item) {
     //I need to access it from here.
}

Then pass your activity as the listener:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(itmMenuitem.getTitle());

builder.setSingleChoiceItems(lstChoices),0, this);

Post a Comment for "How Can I Access My Activity's Instance Variables From Within An AlertDialog's OnClickListener?"