clifford_owino clifford_owino - 3 months ago 8
Android Question

Android Interfaces implementation

I'm having an issue with interfaces, i have a class that implements an interface which i expect to fire after a network response of 200. My interface is not called (how do i hook it to my 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();
}

Answer

I'm not sure if I understand you correctly, but if I did here you are:

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();
        }
    }
}
Comments