Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
92 views
in Technique[技术] by (71.8m points)

android - Callbacks between java and kotlin

Initially, I had API calls in an adapter class which is in Java, I moved the API calls from the adapter to a presenter(Kotlin file).

Adapter(java file) requests to do an API call, from adapter, code moves to presenter (kotlin file) presenter does the actual API call when the response comes or error, adapter methods need to be called.

I implemented callbacks/interface, to do this

Interface:

interface ServiceOrderAdapterInterface {
    fun flOrderOfflineAcceptSuccess(flOrder: FlOrder)
    fun flOrderOfflineAcceptFailure(viewHolder: ServiceOrderAdapter.ViewHolder)
}

Adapter implements the above interface

public class ServiceOrderAdapter extends ArrayAdapter<ServiceOrder> implements ServiceOrderAdapterInterface {
    public class ServiceOrderAdapter extends ArrayAdapter<ServiceOrder> implements ServiceOrderAdapterInterface {
           @Override
        public void flOrderOfflineAcceptSuccess(@NotNull FlOrder flOrder) {
            // Set the current fl activity
            getApp().getFlOrderStateMachine().setCurrentServiceOrder(flOrder);
    
            // Move on to the next screen in the fl activity state machine
            getApp().getFlOrderStateMachine().trigger(FlOrderStateMachine.Events.onAcceptedJob);
        }
    
        @Override
        public void flOrderOfflineAcceptFailure(@NotNull ServiceOrderAdapter.ViewHolder viewHolder) {
            viewHolder.hideLoader();
            app.getServiceOrderBus().post(new ServiceOrderAcceptingEvent(false));
        }
    }
}

Presenter layer - want to call the interface methods

private fun handleFlOrderOfflinceAcceptFailure(failure: Failure) {
    //call the interface method fun flOrderOfflineAcceptSuccess(flOrder: FlOrder)
}

private fun handleFlOrderOfflinceAcceptSuccess(response: GenericResponse) {
    //want to call the interface method fun flOrderOfflineAcceptFailure(viewHolder: ServiceOrderAdapter.ViewHolder)
}

How can I do this?

question from:https://stackoverflow.com/questions/65947391/callbacks-between-java-and-kotlin

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...