【问题标题】:is there an efficient way to pass an array to an asyncTask?有没有一种有效的方法将数组传递给 asyncTask?
【发布时间】:2020-02-10 11:35:29
【问题描述】:

我有一个需要一些时间来执行的函数。目前我在一个线程中运行它,因为我不能让 AsyncTask 工作。我需要将一个 double[] 变量作为输入传递给 doInBackground,然后它应该返回结果,该结果也是一个 double[] 变量到 onPostExecute,结果将在 textView 中打印。
看起来很简单:

public class finder extends AsyncTask <Double , Void , Double> {
    double[] input;
    double[] result;

    public finder() {
        super();
    }


    @Override
    protected double[] doInBackground(Double... params) {

        result = foo(input)

        return result;
    }


    protected void onPostExecute(Double... params) {


    }

public double[] foo (double [] input){...}
}

所以我有几个问题:

1- 类和每个方法的哪些参数配置可以完成这项工作?
2-我需要覆盖这些方法吗? (当我将 doInBackground 说明符更改为 double[] 时,@Override 出现错误)
3- 每种方法的说明符应该是什么?
4-我怎样才能让 onPostExecute 识别 textView?应该将什么传递给 asyncTask ?

【问题讨论】:

    标签: android-asynctask


    【解决方案1】:

    您的问题已在以下链接中得到解答。我想它会对你有所帮助。谢谢 What arguments are passed into AsyncTask<arg1, arg2, arg3>?

    【讨论】:

    • 我已经阅读了文档,如果输入是一个列表或只是一个双变量,一切都会很容易。但我拥有的输入数组是一个数组,其元素应一次性提供给foo(double[])。并且结果数组必须从 doInBack 返回,所以 doInBack 必须声明为 double[] 这给了我一个错误。
    【解决方案2】:

    似乎创建一个只有两个 double[] 成员的类并将该类作为 AsyncTask 参数和 doInBackground 的返回类型传递可以解决问题。

    class mTest {
    
         double[] testresult;
         double [] testinput;
    
    
    }
    

    和:

    private class myAsync extends AsyncTask<mTest, Void , mTest>{
    
    
            mTest localmtest = new mTest();
    
            @Override
            protected mTest doInBackground(mTest...pp) {
                localmtest = pp[0];
                localmtest.testresult = foo(localmtest.testinput);
    
                return localmtest;
            }
    
            @Override
            protected void onPostExecute(mTest ppp) {
    
                outputset(ppp.testresult);
                //outputset sets some TextView text
    
            }
    
    
        }
    

    感谢@Tien hT 的帮助。

    【讨论】:

      猜你喜欢
      • 2019-01-18
      • 1970-01-01
      • 2011-03-18
      • 1970-01-01
      • 1970-01-01
      • 2021-11-19
      • 2014-07-17
      • 1970-01-01
      • 2018-08-04
      相关资源
      最近更新 更多