I have the following problem.
I have a class named “A” in which I create a list “List dStruct” .
I modify this object in the “A” class and I want to pass it to an Activity for further use.
Also the “A” class extends AsyncTask.
In this case,how do I pass an object to an Activity?
Here is the Activity Code:
List dStruct; //the object I want to access
btnPlanTrip.setOnClickListener(new OnClickListener() {
tripReq = new TripRequest(MainActivity.this);
tripReq.execute(request);
dStruct=tripReq.dStruct;
String str= dStruct.get(0).name;
}
Here is the code for the class “A”
public class TripRequest extends AsyncTask {
List dStruct;
public TripRequest(MainActivity activity) {
this.activity = activity;
dStruct=new ArrayList <DirectionsStruct>();
progressDialog = new ProgressDialog(activity);
}
protected void onPostExecute(Long result) {
code for dStruct
}
}
I solved this issue using Handlers. In my custom AsyncTask, I pass a Handler from an Activity to the constructor. In the AsyncTask’s
onPostExecute, I call Handler.sendMessage and it sends whatever data back to the Activity.AsyncTask Code (your “A” class)
Handler declared in Activity
Call to AsyncTask from Activity passing in Handler to ctor (do this in your button click event handler)