2011-04-29 50 views
0
new DownloadFilesTask().execute(myPrefs.getString("IP", ""), null, null); 

我從dobackground方法返回一個Arraylist ..如何將它放入我的ArrayList?Asyc任務返回Arraylist檢索它?

Arraylist al=null; 

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

不工作。

回答

1

我從dobackground方法返回一個Arraylist ..如何將它放入我的ArrayList?

通過返回之前指定從doInBackground()方法當地ArrayList到全局ArrayList

示例代碼:

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

BT這個ASYC類是不是我的班裏面? – Harinder 2011-04-29 07:10:05

+1

@Harinder:我從來沒有得到這些信息,但是當你構造你的'AsyncTask'時,將你的'Activity'對象實例的引用傳遞給'AsyncTask'類的成員字段,並在' Activity',它有一個'ArrayList'作爲參數,並使該方法將'ArrayList'分配給你的'Activity'的全局'ArrayList'。 – Wroclai 2011-04-29 07:11:52

0
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(""); 
1

嘗試像這樣在我們的代碼

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

      } 

那麼你可以將此ArrayList分配到後您的ArrayList執行從

0

返回它的doInBackground方法,並將其綁定到onPostExecute方法中。

在onPostExecute方法,你可以得到doInBackground的返回值作爲參數值