[英]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为了更新我使用 AsyncTask 的每个元素,所以像这样
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.后来,更新我的GUI中的列表,我用notifyDataSetChanged
的BaseAdapter
我的清单。
Is this somehow possible?这有可能吗? How do I need to change my AsyncTask?我需要如何更改我的 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.将完整的 arrayList 和 baseAdapter 对象传递给 asyncTask。 and update the current element of arrayList and then make notifyDatasetchanged()并更新 arrayList 的当前元素,然后进行 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...您可以在 onPreExecute 和 onPostExcute 中更新 asyncTask 中的 ui 组件...
@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. @Prassana 已经向您展示了如何使用构造函数将对象传递给 AsyncTask,但还有另一种优雅的方式来实现,即通过更改 AsyncTask 类类型参数。 If I wanted to pass an ArrayList< String> to AsyncTask, and receive an ArrayList< Integer> back, the class layout would look something like this.如果我想将一个 ArrayList< String> 传递给 AsyncTask,并接收一个 ArrayList< Integer> 返回,类布局将如下所示。
// 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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.