AsyncTask passing custom objects

心不动则不痛 提交于 2019-12-11 23:29:01

问题


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

}


回答1:


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.




回答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

    }
}



回答3:


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



来源:https://stackoverflow.com/questions/18191479/asynctask-passing-custom-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!