1

我有一个 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 异步完成其工作?

4

1 回答 1

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;
}
于 2011-06-08T09:46:39.710 回答