Android接口实现

时间:2016-08-12 13:52:55

标签: java android

我遇到了接口问题,我有一个实现接口的类,我希望在网络响应200后触发。我的接口没有被调用(如何将它挂钩到我的NetworkrequestClass?)

//My class
  public class MyClass extends AnotherClass implements MyDesiredListener{

    private void myMethod(){

       if (!value) {
         NetworkCallClass.specificRequest(); //To call onDesiredLoadedData 
        }
     }

    @Override
    onDesiredLoadedData(){
      //Update Value
     }
   }

   //My inteface in its own file
   interface  MyDesiredListener{
   onDesiredLoadedData();
   }

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解正确,但是如果我在这里你就是:

public class FirstClass implements MyInterface {
    public void method() {
        NetworkCallClass clazz = new NetworkCallClass(this);
        clazz.specificRequest();
    }

    @Override
    public void onDesiredLoadedData() {
        // here you are your callback
    }
}

public class NetworkCallClass {
    MyInterface _callbacks;

    public NetworkCallClass(MyInterface callbacks) {
        _callbacks = callbacks;
    }

    public void specificRequest() {
        // code
        if (networkResponse == 200) {
            _callbacks.onDesiredLoadedData();
        }
    }
}