我有一个针对getView()的Android Gallery ImageAdapter实现,如下所示:
public View getView(int arg0, View arg1, ViewGroup arg2) {
String strURL = "http://app1.exactdev.co.za/android/celebs/celeb" + (arg0+1) + ".jpg";
Bitmap bm = RemoteBitMapHelper.getRemoteBitMap(strURL); //synchronous request
ImageView i = new ImageView(ctx);
i.setImageBitmap(bm);
return i;
}
如何让getView以异步方式完成工作?
答案 0 :(得分:1)
简单的答案是你可以将它放在AsyncTask中。类似于以下内容(未经测试)
public View getView(int arg0, View arg1, ViewGroup arg2) {
final ImageView i = new ImageView(ctx);
String url = "http://app1.exactdev.co.za/android/celebs/celeb" + (arg0 + 1) + ".jpg";
new AsyncTask<String, Void, Bitmap>() {
@Override
protected Bitmap doInBackground(String... urls) {
return RemoteBitMapHelper.getRemoteBitMap(urls[0]);
}
@Override
protected void onPostExecute(Bitmap result) {
if (result != null) {
i.setImageBitmap(result);
}
}
}.execute(url);
return i;
}