如何正确覆盖Runnable.run(),以便它可以采取参数?

问题描述 投票:0回答:1

我目前正在研究一个Android项目并且遇到了这种情况,我必须传递一个函数作为参数,所以我浏览了StackOverflow并试用了解决方案,用Runnable封装了这个函数。

我现在面临的问题是,我希望传递的函数,期望参数和Runnable.run()不需要任何参数。所以我现在正在尝试扩展Runnable并覆盖run()让它接受参数,然后我可以传递给实际的函数。

现在我有点不确定,因为run()用于Thread我如何能够覆盖它,而不会击败Thread。到目前为止,我没有使用任何其他Thread方法。任何建议的方法?

编辑

目标是,在侦听器方法中调用这些自定义Runnables,如下所示:

public void startRequest(RequestOperation operation, final Runnable onSuccess, final Runnable onError, final Runnable onFinished) {

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, instance_context.getResources().getString(R.string.base_url), null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            onSuccess.run();
            onFinished.run();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            onError.run();
            onFinished.run();
        }
    });
    queue.add(request);
}

在最好的情况下,onSuccess能够通过response对象,因为onError获得error

java android override runnable
1个回答
2
投票

为什么强迫Runnable成为它不是设计的东西?只需拥有自己的界面即可拥有所需的签名。例如。

public interface Callback<T> {
    void run(T parameter);
}

public void startRequest(RequestOperation operation, final Callback<JSONObject> onSuccess, final Callback<VolleyError> onError, final Runnable onFinished) {

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, instance_context.getResources().getString(R.string.base_url), null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            onSuccess.run(response);
            onFinished.run();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            onError.run(error);
            onFinished.run();
        }
    });
    queue.add(request);
}

甚至更好(如果你问我):

public interface Callback {
    void onSucces(JSONObject response);
    void onError(VolleyError error);
    void onFinished();
}

public void startRequest(RequestOperation operation, final Callback callback) {

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, instance_context.getResources().getString(R.string.base_url), null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            callback.onSuccess(response);
            callback.onFinished();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            callback.onError(error);
            callback.onFinished();
        }
    });
    queue.add(request);
}

如果需要,您还可以再次制作Callback通用:

public interface Callback<R, E> {
    void onSucces(R response);
    void onError(E error);
    void onFinished();
}

public void startRequest(RequestOperation operation, final Callback<JSONObject, VolleyError> callback) {...}

使用:

public class SimpleCallback implements Callback {
    public void onSucces(JSONObject response) {
        doSomethingWithResponse(response);
    }

    public void onError(VolleyError error) {
        doSomethingWithError(error);
    }

    void onFinished() {
        logFinishTime();
    }
}

startRequest(operation, new SimpleCallback());
© www.soinside.com 2019 - 2024. All rights reserved.