I have an AsynkTask method on my MainActivity that it\'s the following:
class chargeCars extends AsyncTask> {
Am I doing something wrong? Why in my doInBackground it gives me the correct size of the ArrayList and in my onCreate method not?
Because the execution is asynchronous and the UI Thread
doesn't way the end of the execution of your AsyncTask
to continue its execution flow. To get the results back, when your AsyncTask
finishes its execution, without blocking the UI Thread
, you could use a delegate/lister.
Let's declare an interface on the top of your Activity, and let't it implement this interface (you'll need to implement the methods). When onCarsCompleted
is invoked you'll be able to print the correct size of your ArrayList
public class MainActivity extends ActionBarActivity implements OnCarsListener {
public interface OnCarsListener {
void onCarsCompleted(ArrayList<Car> cars);
void onCarsError(String error);
}
// the other code
public void onCarsCompleted(ArrayList<Car> cars) {
Log.d("Size: ", "" + cars.size());
}
public void onCarsError(String error) {
}
}
now we add a constructor to your AsyncTask
, which takes as parameter an object of type OnCarsListener
, and we keep a member reference to the object in the scope of chargeCars
, and onPostExecute
we communicate back to the Activity
the results of your computation:
class chargeCars extends AsyncTask<Void, Integer, ArrayList<Car>> {
private final OnCarsListener mListener;
ArrayList<Car> cars = new ArrayList<Car>();
public chargeCars(OnCarsListener listener) {
mListener = listener;
}
protected void onPreExecute(){
}
protected ArrayList<Car> doInBackground(Void... params) {
cars.add(new Car(1,"Car1");
cars.add(new Car(2,"Car2");
cars.add(new Car(3,"Car3");
Log.d("Size: ", "" + cars.size()); //It gives me the size equals to 3 -->Correct
return cars;
}
protected void onProgressUpdate(){
}
protected void onPostExecute(ArrayList<Car> cars){
if (mListener != null) {
mListener.onCarsCompleted(cars);
}
}
}
And of course, you need to instantiate the AsyncTask like
new chargeCars(this).execute();
Method 1: Don't use clone just use object to add:
protected void onPostExecute(ArrayList<Car> c)
{
super.onPostExecute();
for(Car cr: c)
{
totalCars.add(cr);
}
//Code for further execution
}
Method 2: Make a method which will be called by onPostExceute() after it has got ArrayList
protected void onPostExecute(ArrayList<Car> c)
{
super.onPostExecute();
setValues(c);
}
private void setValues(ArrayList<Car> c){
for(Car cr: c)
{
totalCars.add(cr);
}
//Code for further execution
}