0

[I have a custom parcelable object Vehicle]

I have looked at AsyncTask but it wasn't too clear on this topic:

I would like to pass a String(which is the Vehicle ID of the Vehicle) into an AsyncTask, then in doInBackground() I have a method

mRemoteInterface.getTrackHistory();

which gives me an ArrayList. I would like to then, in onPostExecute() start an activity where both the Vehicle ID and ArrayList are extras.

This is an outline of what I wan't to be able to do. The issue is that I don't understand passing objects INTO the asynctask, and then from doInBackground() to onPostExecute() and from onPostExecute() back to the original execute call.

getTrackHistory.execute(WHAT SHOULD GO 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);
    }

}

benzabill
  • 259
  • 1
  • 8
  • 21

3 Answers3

5

You can pass the string to the constructor of asynctask or to doInbackground

   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 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 am assuming its ArrayList of type String. Change the arraylist to what suits your requirement.

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 
}

In your activity class implement the interface

  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
 }

Similar post using interface

Android Parse JSON stuck on get task.

eoinzy
  • 2,152
  • 4
  • 36
  • 67
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Thank you, that makes sense. Could you elaborate on passing from onPostExecute() back to where new getTrackhistory(mystringvalue).execute() was called?(is this possible) – benzabill Aug 12 '13 at 16:03
  • @bent you can do that with the help of a interface. i have answered a similar one. i will find the link – Raghunandan Aug 12 '13 at 16:04
  • Ah, thank you. So I can pass a value to either doInBackground() or to a constructor. Effectively the same. Thanks @Raghunandan!!! – benzabill Aug 12 '13 at 16:12
  • I am having an issue that I am hoping you can help me with. Could you take a look? http://stackoverflow.com/questions/18448216/unable-to-execute-dex-multiple-dex-files-define-lcom-shared-classes Thank you! – benzabill Aug 26 '13 at 16:06
2

Common approach to this is to pass your arguments/objects to constructor of your AsyncTask, store as member and then reference from doInBackground(). For example:

private class getTrackHistory extends AsyncTask<String, Integer, Boolean> {

    Vehicle mVehicle;

    public getTrackHistory( Vehicle v) {
       mVehicle = v;
    }

    @Override
    protected Boolean doInBackground() {

          // use mVehicle member here

    }
}
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
0

An Asynctask is just a class like any other, use a constructor, or populate the instantiated object through a setter, public members, etc.

Sam Dozor
  • 40,335
  • 6
  • 42
  • 42