2011-09-17 23 views
0

我正在Android應用程序中實現網站的API。這個API的一部分給了我一個數組,其中包含一組圖像URL。現在,我可以很好地遍歷數組,並且我寫了一個AsyncTask,它將圖像URL轉換爲drawable。但我沒有一個很好的辦法讓這些可繪製成ImageView的(這我在迴路中產生編程):AsyncTask結果到本地變量

void createPhotoPost(JSONObject postData, LinearLayout myPostHolder) { 
    try { 
     ImageView myImage = new ImageView(myContext); 
     JSONArray photosArray = new JSONArray(postData.getString("photos")); 
     for(int i = 0; i < photosArray.length(); i++) { 
      JSONObject thisPhoto = photosArray.getJSONObject(i); 
      JSONArray sizesArray = new JSONArray(thisPhoto.getString("alt_sizes")); 
      JSONObject largest = sizesArray.getJSONObject(0); 
      new GetPhotoSet(this).execute(largest.getString("url")); 
     } 
     myPostHolder.addView(myImage); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

我上面貼的功能是從另一個循環中調用。正如你在上面的代碼中看到的那樣,ImageView myImage需要最終包含由GetPhotoSet.execute的執行調用返回的drawable。有任何想法嗎?謝謝!

附加信息:由於AsyncTask返回數據的方法在我聲明ImageView myImage的循環之外,它不知道myImage存在,並且它不能引用它(並且存在據推測這是少數myImages從這一點,從循環的不同迭代)

回答

3

尼克,這就是onPostExecute()是什麼。在onPostExecute()中創建您的ImageView並從那裏將其添加到您的Activity。如果您正在循環播放一系列圖像,則還可以使用publishProgress()執行此操作。在你的例子中,你應該把整個循環移到你的任務的doInBackground()

+0

太棒了,非常有意義。謝謝! – Nick