开发者

Asyc Task return Arraylist retrieve it?

new DownloadFilesTask().execute(myPrefs.getString("IP", ""), null, null);

I returns an Arraylist from the dobackground method..how to put it into my arraylist?

Arraylist al=null;

al=new DownloadFilesTask().execute(myPrefs.getString("IP", "开发者_运维技巧"), null, null);

not working.


I returns an Arraylist from the dobackground method..how to put it into my arraylist?

By assigning your local ArrayList from the doInBackground() method to your global ArrayList before returning it.

Sample code:

@Override
protected ArrayList doInBackground(...) {
     ArrayList localArrayList = new ArrayList();
     // Other stuff you might have.
     // ...
     return mYourGlobalArrayList = localArrayList;
}


Try like this in ur code

//After Preexecute...


@Override
                protected ArrayList doInBackground(...) {
                     ArrayList localArrayList = new ArrayList();
                     // Other stuff you might have.
                     // ...
                     return localArrayList;
                }
                @Override
                protected void onPostExecute(Object result) {
                    // TODO Auto-generated method stub

                    if(result!= null)
                    {
                        ArrayList<String> category = new ArrayList<String>();
                        category = (ArrayList<String>) result;
                    }
                }

            }

Then You can assign this arraylist to your arraylist in post execute


Return it from your doInBackground method, and bind that in onPostExecute method.

In onPostExecute method, you can get return value of doInBackground as parameter value


new AsyncTask<String, Void, ArrayList<String>>() {

            @Override
            protected ArrayList<String> doInBackground(String... params) {
                //result = do some work
                            ArrayList localArrayList = new ArrayList();
                         //   localArrayList = get from your website or from database
                          // assign arraylist globally
                return result;
            }


        }.execute("");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜