簡體   English   中英

將對象傳遞給 AsyncTask

[英]Pass an object to AsyncTask

我有一個具有某些狀態的對象列表

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

該列表中的每個對象都包含一個特定的值,可以通過互聯網更新(比如活動的、非活動的)。 為了更新我使用 AsyncTask 的每個元素,所以像這樣

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

后來,更新我的GUI中的列表,我用notifyDataSetChangedBaseAdapter我的清單。

這有可能嗎? 我需要如何更改我的 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;
    }
}

將完整的 arrayList 和 baseAdapter 對象傳遞給 asyncTask。 並更新 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();
    }
} 

您可以在 onPreExecute 和 onPostExcute 中更新 asyncTask 中的 ui 組件...

@Prassana 已經向您展示了如何使用構造函數將對象傳遞給 AsyncTask,但還有另一種優雅的方式來實現,即通過更改 AsyncTask 類類型參數。 如果我想將一個 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> 

請注意,這是主要結構,只是為了給您一個想法。 我不能保證這是無錯的代碼。 祝你好運!

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

當我們執行異步任務時,使用此代碼發送對象

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM