How can I return an ArrayList from my AsyncTask?

后端 未结 2 842
一整个雨季
一整个雨季 2020-12-15 13:17

I have an AsynkTask method on my MainActivity that it\'s the following:

class chargeCars extends AsyncTask> {
              


        
2条回答
  •  攒了一身酷
    2020-12-15 14:12

    Method 1: Don't use clone just use object to add:

    protected void onPostExecute(ArrayList 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 c)
        {
         super.onPostExecute();
           setValues(c);
        }
    
    private void setValues(ArrayList c){
          for(Car cr: c)
            {
                totalCars.add(cr);
            }
     //Code for further execution
    }
    

提交回复
热议问题