AsyncTask 的结果存入局部变量
我正在致力于在 Android 应用程序中实现网站的 API。该 API 的一部分为我提供了一个数组,其中包含图像 URL 的数组。现在,我可以很好地迭代数组,并且我已经编写了一个 AsyncTask,可以将图像 URL 转换为可绘制对象。但我没有一个好的方法将这些可绘制对象放入 ImageView(我在循环中以编程方式创建):
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 的执行调用返回的可绘制对象。有什么想法吗?谢谢!
其他细节:由于 AsyncTask 返回数据的方法位于我声明 ImageView myImage 的循环之外,因此它不知道 myImage 存在,并且无法引用它(并且可能有一些myImages 此时,来自循环的不同迭代)
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 is 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尼克,这就是
onPostExecute()
的用途。在onPostExecute()
中创建您的ImageView
并将其添加到您的Activity
中。如果您要循环访问一系列图像,也可以使用publishProgress()
来执行此操作。在您的示例中,您应该将整个循环移至任务的doInBackground()
。Nick, that's what
onPostExecute()
is for. Create yourImageView
inonPostExecute()
and add it to yourActivity
from there. If you are looping over a series of images, you can also usepublishProgress()
to do this. In your example you should move the entire loop to your task'sdoInBackground()
.