How to implement a listener

前端 未结 2 1051
日久生厌
日久生厌 2020-12-04 13:41

I have a design issue. I need to implement a listener. I saw the following SO question: How to create our own Listener interface in android?

But in the link it provi

相关标签:
2条回答
  • 2020-12-04 14:15

    AsyncTask has nothing to do with implementing a listener.

    Here's a listener:

    public interface TheListener {
        public void somethingHappened();
    }
    

    Call it however you want. For example, here's a class doing something like View:

    public class Something {
        private TheListener mTheListener;
    
        public void setTheListener(TheListener listen) {
            mTheListener = listen;
        }
    
        private void reportSomethingChanged() {
            if (mTheListener != null) {
                mTheListener.somethingHappened();
            }
        }
    }
    

    You can make this as complicated as you want. For example, instead of a single listener pointer you could have an ArrayList to allow multiple listeners to be registered.

    Calling this from native code also has nothing to do with implementing a listener interface. You just need to learn about JNI to learn how native code can interact with Java language code.

    0 讨论(0)
  • 2020-12-04 14:32

    Just to clear things up;

    you do exactly what @hackbod said and add this :

    the activity which encloses the class (with the method setListener(Listener listen)), implements Listener and in it's oncreate or onResume or where-ever you call yourClass.setListener(this).

    0 讨论(0)
提交回复
热议问题