How to pass an object to my AsyncTask?

I have a Car object that has this constructor:

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

I have no problems here, so I created one Car object named newCar , for example:

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

The problem is that I want to pass this newCar to my AsyncTask called modifyCar as a parameter, but I don't know how to do it.

I searched in SO and I found this question: AsyncTask passed custom objects but the problem is not resolved, because in the solution it is given, they pass String only to AsyncTask , and not completely to the object.

I want it to pass the entire object as a parameter to AsyncTask.

In accordance with the solution that was asked in the question I posed above, I tried to do this in order to pass the object to AsyncTask .

 new modifyCar(newCar).execute(); 

So, I declared AsyncTask as follows:

 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() { } } 

But I do not know if it is right or not. If not, what should I do for this purpose?

Thanks in advance!

+4
source share
3 answers

The solution you are reading is right, you just did it wrong. You needed to easily create a constructor for the AsyncTask class and pass an object to it.

 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() { } } 

and execute this class

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

It doesn’t matter if your object is an instance of String or Car or AbstractDeathRayController , the expected way to pass them to AsyncTask via the execute method:

 new modifyCar().execute(car); 

BTW, the Java convention for class names is to use CamelCase, so it would be nice to rename your class to ModifyCar .

+3
source

You must pass the Car object in the execution method. You can read it in the documentation ( http://developer.android.com/reference/android/os/AsyncTask.html ):

An asynchronous task is defined by three types called Params, Progress and Result, and 4 steps called onPreExecute, doInBackground, onProgressUpdate and onPostExecute

Follow the sample documentation:

 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"); } } 

And call the async task with:

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

All Articles