hsz
hsz

Reputation: 152236

Access an object from listener

Let's say we have some Foo class that contains

public void setOnSomethingListener(OnSomethingListener listener);

We register it with:

Foo foo = new Foo();
foo.setOnSomethingListener(new OnSomethingListener(){
  public void onSomething(String data) {
    // ...
  }
});

Is it possible to access from onSomething method parental object that has registered mentioned listener without accessing defined foo value ?

I ask about it because I would like to assign this listener to some parameter and register it in the few objects.

Upvotes: 3

Views: 2780

Answers (2)

Vaim
Vaim

Reputation: 61

Try

OnSomethingListener.this.*

Upvotes: 0

SJuan76
SJuan76

Reputation: 24885

Yes.

What you are doing here is defining an anonymous inner class. You can access attributes and methods of the containing class by doing.

MyContainingClass.this.attributeName

Upvotes: 6

Related Questions