【问题标题】:How can I pass an object to my AsyncTask?如何将对象传递给我的 AsyncTask?
【发布时间】:2015-08-06 17:11:18
【问题描述】:

我有一个对象Car,它有这个构造函数:

public Car(int idCar, String name)
{
    this.idCar = idCar;
    this.name = name;
}

在这里我没有任何问题,所以我创建了一个名为Car 的对象newCar,如下所示:

Car newCar = new Car(1,"StrongCar");

我遇到的问题是我想将这个newCar 传递给我的AsyncTask,命名为modifyCar 作为参数,但我不知道该怎么做。

我在 SO 中进行了搜索,发现了这个问题:AsyncTask passing custom objects 但它并没有解决我的问题,因为在给出的解决方案中,它们只将String 传递给AsyncTask,而不是整个对象。

我希望将整个对象作为参数传递给 AsyncTask。

根据我在上面提出的问题中给出的解决方案,我尝试这样做以将对象传递给AsyncTask

new modifyCar(newCar).execute();

所以我这样声明了 AsyncTask:

class modifyCar extends AsyncTask<Car, Integer, ArrayList<Evento>> {
 protected void onPreExecute()
 {
 }

 protected ArrayList<Evento> doInBackground(Car... newCarAsync) 
 {
     //The rest of the code using newCarAsync
 }

 protected void onProgressUpdate()
 {
 }

 protected void onPostExecute()
 {
 }
}

但我不知道它是否正确。如果没有,我应该为此做些什么?

提前致谢!

【问题讨论】:

    标签: java android android-asynctask


    【解决方案1】:

    您阅读的解决方案是正确的,您只是做错了。您需要轻松地为您的 AsyncTask 类创建一个构造函数并将对象传递给它

    class modifyCar extends AsyncTask<Void, Integer, ArrayList<Evento>> {
        private Car newCar;
    
        // a constructor so that you can pass the object and use
        modifyCar(Car newCar){
            this.newCar = newCar;
        }
    
        protected void onPreExecute()
        {
        }
    
        protected ArrayList<Evento> doInBackground(Void... parms) 
        {
            //The rest of the code using newCarAsync
        }
    
        protected void onProgressUpdate()
        {
        }
    
        protected void onPostExecute()
        {
        }
    }
    

    并执行这个类

    // pass the object that you created
    new modifyCar(newCar).execute();
    

    【讨论】:

    • 但是,在doInBackground() 方法中,如何在其中使用对象Car?或者newCarAsyncAsyncTask中声明的对象newCar一样?
    • @Error404 您创建的对象现在被传递给类,在类内部或doInBackground() 它是newCar
    • 那么,newCarAsync 变量在doInBackground() 方法中的作用是什么?
    • @Error404 没用,我只是不想改你的类,我编辑了上面的类
    • 非常感谢!你帮了我很多! ;)
    【解决方案2】:

    如果您的对象是StringCarAbstractDeathRayController 的实例,则没有区别,将它们传递给AsyncTask 的预期方法是通过execute 方法:

    new modifyCar().execute(car);
    

    顺便说一句,Java convention for class names 使用 CamelCase,因此将您的类重命名为 ModifyCar 可能是个好主意。

    【讨论】:

    • doInBackground() 中展示如何访问它可能会很好,因为有些人很难理解可变参数。
    • 正如@DanielNugent 所说,我需要知道如何在doInBackground() 方法中访问它,因为我不太明白你到底想说什么。
    【解决方案3】:

    您应该将 Car 对象传递给 execute 方法。你可以在文档中阅读它(http://developer.android.com/reference/android/os/AsyncTask.html):

    异步任务由 3 种泛型类型定义,称为 Params, Progress 和 Result,以及 4 个步骤,分别称为 onPreExecute、doInBackground、 onProgressUpdate 和 onPostExecute

    按照文档示例:

    private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
         protected Long doInBackground(URL... urls) {
             int count = urls.length;
             long totalSize = 0;
             for (int i = 0; i < count; i++) {
                 totalSize += Downloader.downloadFile(urls[i]);
                 publishProgress((int) ((i / (float) count) * 100));
                 // Escape early if cancel() is called
                 if (isCancelled()) break;
             }
             return totalSize;
         }
    
         protected void onProgressUpdate(Integer... progress) {
             setProgressPercent(progress[0]);
         }
    
         protected void onPostExecute(Long result) {
             showDialog("Downloaded " + result + " bytes");
         }
     }
    

    然后调用异步任务:

     new DownloadFilesTask().execute(url1, url2, url3);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-11
      • 1970-01-01
      • 2019-06-01
      • 2015-08-03
      • 2015-06-16
      • 1970-01-01
      • 2013-07-13
      相关资源
      最近更新 更多