Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将对象传递给AsyncTask?_Java_Android_Android Asynctask - Fatal编程技术网

Java 如何将对象传递给AsyncTask?

Java 如何将对象传递给AsyncTask?,java,android,android-asynctask,Java,Android,Android Asynctask,我有一个对象Car,它具有以下构造函数: public Car(int idCar, String name) { this.idCar = idCar; this.name = name; } 在这里我没有任何问题,所以我创建了一个名为newCar的对象Car,如下所示: Car newCar = new Car(1,"StrongCar"); class modifyCar extends AsyncTask<Car, Integer, ArrayList<E

我有一个对象
Car
,它具有以下构造函数:

public Car(int idCar, String name)
{
    this.idCar = idCar;
    this.name = name;
}
在这里我没有任何问题,所以我创建了一个名为
newCar
的对象
Car
,如下所示:

Car newCar = new Car(1,"StrongCar");
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()
 {
 }
}
我的问题是我想把这个
新车
作为参数传递给我的
异步任务
,名为
modifyCar
,但我不知道怎么做

我在SO中搜索,发现了以下问题: 但它并没有解决我的问题,因为在给出的解决方案中,它们只将
字符串
传递给
异步任务
,而不是整个对象

我想要的是将整个对象作为参数传递给AsyncTask。

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

new modifyCar(newCar).execute();
因此,我声明AsyncTask如下:

Car newCar = new Car(1,"StrongCar");
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()
 {
 }
}
class modifyCar扩展异步任务{
受保护的void onPreExecute()
{
}
受保护的ArrayList doInBackground(汽车…newCarAsync)
{
//其余代码使用newCarAsync
}
受保护的void onProgressUpdate()
{
}
受保护的void onPostExecute()
{
}
}
但我不知道这是否正确。如果没有,我应该为此做些什么


提前谢谢

如果对象是
String
Car
AbstractDeathRayController
的实例,则将它们传递给
异步任务的预期方式是通过以下方法:


顺便说一句,Java将使用CamelCase,因此最好将类重命名为
ModifyCar

您应该在execute方法上传递Car对象。您可以在文档()中阅读它:

异步任务由3种泛型类型定义,称为Params, 进度和结果,以及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");
     }
 }

你读到的解决方案是对的,你只是做错了。您需要轻松地为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()
    {
    }
}

也许可以在
doInBackground()
中演示如何访问它,因为有些人很难理解varargs。正如@DanielNugent所说,我需要知道如何在
doInBackground()
方法中访问它,因为我不太明白您到底想说什么。但是,在
doInBackground()
方法中,如何在其中使用对象
汽车
?或者
newCarAsync
它与在
AsyncTask
中声明的对象
newCar
相同?@Error404您创建的对象现在被传递到类内部的类或
doInBackground()
它是
newCar
doInBackground()
方法中的
newCarAsync
变量的用途是什么?@Error404没用,我只是不想更改你的类,我在上面编辑了这个类,非常感谢!你帮了我很多
// pass the object that you created
new modifyCar(newCar).execute();