开发者

Results of AsyncTask into local variable

I'm working on implementing a website's API in an Android app. There's a part of this API which gives me an array, which contains an array of image URLs. Now, I can iterate through the arrays just fine, and I have written an AsyncTask that transforms image URLs into drawables. But I don't have a good way to get those drawables into the ImageView (which I am creating programatically in the loop):

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

The function I posted above i开发者_Go百科s called from within another loop. As you can see in my code above, the ImageView myImage needs to eventually contain the drawable that is returned by the execute call of GetPhotoSet.execute. Any ideas? Thanks!

Additional details: Since the method where the data is returned by the AsyncTask is outside of the loop where I declared the ImageView myImage, it doesn't know that myImage exists, and it can't reference it (and there are presumably a handful of myImages by this point, from different iterations of the loop)


Nick, that's what onPostExecute() is for. Create your ImageView in onPostExecute() and add it to your Activity from there. If you are looping over a series of images, you can also use publishProgress() to do this. In your example you should move the entire loop to your task's doInBackground().

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜