1

I am struggling trying to google this subject. I must not being using the correct search terms. I have a location listener, in Android, and I usually nest the listener inside the class I am using it for.

public class ClassName {
    ...
    private class Listener implements SomeListener{
        ...
        public void somethingChanged(){
            ...
        }
    }
}

This time I decided to make the listener a new class to make the code easier to read and so I can reuse it. So now my listener and the class that is using the listener are separate.

public class ClassName{
    ...
}

public class Listener implements SomeListener{
    ...
    public void somethingChanged(){
        ...
    }
}

So inside the somethingChanged() method, I just want to notify my main class that an event has occurred so I can do some updating. I can't think of a way to do this. I can't put anything specific in the listener's method or it is not reusable. How do I make a method that has been called from a Listener call a method in the class using the listener?

4

1 回答 1

2

好吧,你看,内部类知道外部类,因为你分离了类,所以不再是这种情况了。

我建议将 的实例传递ClassNameListener构造函数中的 a ,并从内部调用ClassName的公共方法somethingChanged

于 2012-06-26T15:54:42.090 回答