java - 将多个值传递给 AsyncTask 类

标签 java android android-asynctask

我在尝试将 String 和对象传递给 AsyncTask 类时遇到一些问题。因此,当我单击按钮时,它应该将 String 和 EventReview 对象传递到 AsyncTask 类中:

viewDtlEventBtn.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            new GetEventDetailAsyncTask(new GetEventDetailAsyncTask.OnRoutineFinished() {
                public void onFinish() {
                    //Get the values returned from AsyncTask and pass it to another activity
                }
            }).execute(String.valueOf(eventIDTV.getText()));
        }
    });

在我的 AsyncTask 类中,我将 String 作为参数:

public static class GetEventDetailAsyncTask extends AsyncTask<String, Integer, Double> {
    EventController eventCtrl = new EventController();
    Context context;

    public interface OnRoutineFinished { // interface
        void onFinish();
    }

    private OnRoutineFinished mCallbacks;

    public GetEventDetailAsyncTask(OnRoutineFinished callback) { 
        mCallbacks = callback;
    }

    public GetEventDetailAsyncTask() {
    } // empty constructor to maintain compatibility

    public GetEventDetailAsyncTask(Context context){
        this.context = context;
    }

    @Override
    protected Double doInBackground(String... params) {
        try {
            eventCommentModel = eventCtrl.getEventCommentByID(params[0]);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(Double result) {
        if (mCallbacks != null)
            mCallbacks.onFinish(); // call interface on finish

    }

    protected void onProgressUpdate(Integer... progress) {
    }
}

所以我想知道是否有任何可能的方法将 String 和 EventReview 对象传递给execute(),然后当 doInBackground() 时,每个方法执行每个方法。有什么指南吗?

提前致谢。

最佳答案

您可以在 asynctask 的 Object[] 中传递 String 和自定义类的对象。

Object[] obj = new Object[2];
obj[0] = "my data";
obj[1] = myEventReviewObj;
new GetEventDetailAsyncTask().execute(obj);

异步任务:

public static class GetEventDetailAsyncTask extends AsyncTask<Object, Integer, Double> {


    @Override
    protected Double doInBackground(Object... params) {
    String paramStr = "";
    EventReview eventReview = null;
        if(params[0] instanceof String && params[1] instanceof EventReview) {
            paramStr = (String) params[0];
            eventReview = (EventReview) params[1];
        }
        else {
            eventReview = params[0];
            paramStr = params[1];
        }

        try {
            //perform operation using String and Object as per your need
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}

希望这有帮助。

关于java - 将多个值传递给 AsyncTask 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26958293/

相关文章:

java - 使用元数据唯一标识文件

java - 知道akka actor存在的三种方法

android - 如何返回到android中的父 Activity ?

java - 将多个不同类型的参数传递给 doInBackground (Params...params) 方法的最正确方法是什么?

android - 静态异步任务进度条

android - AsyncTask 线程规则 - 真的只能使用一次吗?

java - 使用 Spring Boot 的基于名称的虚拟主机

java - 按下退出键时取消表选择

java - 无法保存 Eclipse .java 和 .xml 文件中的更改

android - 通知不显示 firebase Android 8