简体   繁体   中英

Pass an object to AsyncTask

I have a list of objects with certain states

private ArrayList<MyObjectMap> MyList;
MyList = new ArrayList<>();

Every object in that list contains a certain value, that can be updated via the internet (say active, inactive). To update each element I use an AsyncTask, so something like this

for(int i=0;i<MyList.size();i++) {
   new myAsyncTask(MyList.get(i)).execute();
}

Later, to update the List in my GUI, I use notifyDataSetChanged for the BaseAdapter of my list.

Is this somehow possible? How do I need to change my AsyncTask?

public class myAsyncTask extends AsyncTask<Void, Void, Void> {
    private MyObjectMap myObject;
    protected void onPreExecute() {

    }
    public myAsyncTask(MyObjectMap mom) {
         myObject = mom;
    }

    @Override
    protected Void doInBackground(Void... voids) {
        myObject.updateThisItem();
        return null;
    }
}

Pass the full arrayList and baseAdapter object to the asyncTask. and update the current element of arrayList and then make notifyDatasetchanged()

public class MyAsyncTask extends AsyncTask<Void, Void, Void> 
{
    private ArrayList<MyObjectMap> myList; 
    private BaseAdapter baseAdapter;
    private int position;

    public MyAsyncTask(ArrayList myList, int position, BaseAdapter baseAdapter) {
         this.myList = myList;
         this.position = position;
         this.baseAdapter = baseAdapter;
    }

    protected void onPreExecute() 
    { 
 
    } 
     
 
    @Override 
    protected Void doInBackground(Void... voids) 
    {
        myList.get(position).updateThisItem(); //or 

        /*    
        MyObjectMap mop = myList.get(position);
        mop.updateThisItem();

        myList.remove(position);
        myList.add(position, mop);
        */
        return null; 
    } 

    protected void onPostExecute() 
    {
        baseAdapter.notifyDataSetChanged();
    }
} 

you can update ui components in asyncTask in both onPreExecute and onPostExcute...

@Prassana has already shown you how to pass objects to AsyncTask by using a constructor, but there is another elegant way to do it, by altering the AsyncTask class type paramers. If I wanted to pass an ArrayList< String> to AsyncTask, and receive an ArrayList< Integer> back, the class layout would look something like this.

// notice the return type and parameter type
public class myAsyncTask extends AsyncTask <ArrayList<String>, Void, ArrayList<Integer> { 

...

// Make sure this method receives and returns the correct types.
// the params are specified when you make the call to execute the asynctask and 
// are accessed in a usual varargs way (like an array).
@Override
public ArrayList<Integer> doInBackground(ArrayList<String>... params) { 

... 

// onPostExecute takes the ArrayList returned by doInBackground
@Override
public void onPostExecute(ArrayList<Integer> result) {

// do something with your newly acquired ArrayList<Integer> 

Note that this is the main structure, just to give you an idea. I cannot garauntee that this is typo-less code. Good luck!

 public class myAsyncTaskextends AsyncTask<Object, Void, Void> {
        private MyObjectMap myObject;
        protected void onPreExecute() {

        }


        @Override
        protected Void doInBackground(Object... voids) {
           myObject = (MyObjectMap)param[0];
            myObject.updateThisItem();
            return null;
        }
    }

when we execute asyn task use this code to send object

Asyntask.execute(loginRequestBean);

inside background method you can get object like this

@Override
    protected Object doInBackground(Object... params) {
        Object responseObject = null;
        try {


            Object object = params[0];
            if (object instanceof LoginRequestBean) {
                //login webservice
            } else if (object instanceof RegisterBean) {
                //registration webservice
            }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM