AsyncTask passes custom objects

[I have a convenient vehicle facility]

I looked at AsyncTask , but this topic was not too clear:

I would like to pass String (which is the vehicle identifier) ​​to AsyncTask and then to doInBackground () I have a method

mRemoteInterface.getTrackHistory(); 

which gives me an ArrayList. I would like onPostExecute () to trigger an action in which Vehicle IDs and ArrayList are optional.

This is a diagram of what I cannot do. The problem is that I don’t understand how to pass INTO objects to asynctask and then from doInBackground () to onPostExecute () and fromPostExecute () back to the original execution call.

getTrackHistory.execute ( WHAT SHOULD BE HERE? );

 private class getTrackHistory extends AsyncTask<String, Integer, Boolean **WHAT SHOULD GO HERE?**> { @Override protected Boolean doInBackground( String... params) { try { String vehicleID = params[0]; listVehicleHistory = (ArrayList<Vehicle>) mRemoteInterface.getVehicleHistory(vehicleID); } catch (Exception e) { e.printStackTrace(); } } @Override protected void onProgressUpdate(Integer... progress) { } @Override protected void onPostExecute(Boolean worked) { super.onPostExecute(worked); Intent intent = new Intent(MainActivity.this,VehicleInfo.class); intent.putExtra("Vehicle ID", toReturn); intent.putParcelableArrayListExtra("VehicleHistory", listVehicleHistory); startActivity(intent); } 

}

0
source share
3 answers

You can pass the string to the asynctask or doInbackground constructor

  new getTrackHistory(mystringvalue).execute(); 

Then in the constructor

  private class getTrackHistory extends AsyncTask<String, Integer, Boolean> { String value; public getTrackHistory(String mystring) { value = mystring; } 

Edit:

You can also pass the value to doInbackground()

  new TheTask().execute("mystring"); class TheTask extends AsyncTask <String,Void,Void> { @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); } @Override protected Void doInBackground(String... params) { String value = params[0]; return null; } } 

To the question in the comment

  new getTrackHistory(mystringvalue,ActivityName.this).execute(); 

In the constructor

  String value; TheInterface listener; public getTrackHistory(String mystring,Context context) { value= mystring; listener = (TheInterface) context; } 

Interface

 public interface TheInterface { public void theMethod(ArrayList<String> result); // your result type } 

then

In your doInbackground return the result. I assume its ArrayList is of type String. Change arraylist to suit your requirements.

In your onPostExecute

 if (listener != null) { listener.theMethod(result); // result is the ArrayList<String> // result returned in doInbackground // result of doInbackground computation is a parameter to onPostExecute } 

Implement an interface in your activity class

  public class ActivityName implements getTrackHistory.TheInterface 

then

  @Override public void theMethod(ArrayList<String> result) { // change the type of result according yo your requirement // use the arraylist here } 

A similar post using the interface

Android Parse JSON stuck on get job .

+5
source

A general approach to this is to pass your arguments / objects to the constructor of your AsyncTask , save as a member, and then the link from doInBackground() . For instance:

 private class getTrackHistory extends AsyncTask<String, Integer, Boolean> { Vehicle mVehicle; public getTrackHistory( Vehicle v) { mVehicle = v; } @Override protected Boolean doInBackground() { // use mVehicle member here } } 
+2
source

Asynctask is just a class, like any other, to use a constructor or fill an instance of an object through a setter, public elements, etc.

0
source

All Articles