将对象传递给AsyncTask

时间:2015-11-16 10:15:44

标签: java android android-asynctask

我有一些具有特定状态的对象列表

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

该列表中的每个对象都包含一个特定值,可以通过互联网更新(比如活动,非活动)。要更新每个元素,我使用AsyncTask,所以像这样

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

稍后,要在我的GUI中更新列表,我将notifyDataSetChanged用于我的列表的BaseAdapter

这有可能吗?我如何更改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;
    }
}

4 个答案:

答案 0 :(得分:3)

将完整的arraylist和baseAdapter对象传递给asyncTask。并更新arrayList的当前元素,然后生成notifyDatasetchanged()

public class myAsyncTaskextends 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();
    }
} 

你可以在onPreExecute和onPostExcute中更新asyncTask中的ui组件......

答案 1 :(得分:3)

@Prassana已经向您展示了如何使用构造函数将对象传递给AsyncTask,但是通过更改AsyncTask类类型参数,还有另一种优雅的方法。如果我想传递一个ArrayList&lt;串GT;到AsyncTask,并接收一个ArrayList&lt;整数&GT;回来,班级布局看起来像这样。

// 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> 

请注意,这是主要结构,只是为了给你一个想法。我无法理解这是没有错字的代码。祝你好运!

答案 2 :(得分:0)

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

答案 3 :(得分:0)

当我们执行asyn任务时,使用此代码发送对象

Asyntask.execute(loginRequestBean);

在背景方法中你可以得到像这样的对象

@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
            }
}