テキストと 1 つの ImageView を持つアイテムを含む ListView があります。ImageView (遅延ロード) をロードするために AsyncTask を使用したいと思います。それを行うためにいくつかのライブラリがあることを私は知っています。問題は、次のように URL だけで画像を取得できないことです。
imageLoader.displayImage(imageUri, imageView);
HTTP GET (認証トークン) にいくつかのヘッダーを追加する必要があるためです。したがって、アダプターの getView() 内で、画像をビットマップとしてダウンロードして ImageView に表示する AsyncTask を起動する必要があります。
コード例はありますか?AsyncTask のコンストラクターで ImageView を AsyncTask に渡し、ビットマップをダウンロードして onPostExecute の ImageView に割り当てようとしました。
public class LoadImageTask extends AsyncTask<String, Void, Bitmap>{
private ImageView mImage;
private Context mContext;
private String imageId;
private Throwable mThrown = null;
public LoadImageTask(ImageView ivImage, Context context) {
mImage = ivImage;
mContext = context;
}
@Override
protected Bitmap doInBackground(String... params) {
imageId = params[0];
// Build request and request token
....
//Process the response
try {
HttpEntity entity = mHttpResponse.getEntity();
InputStream inputStream = entity.getContent();
BufferedInputStream bis = new BufferedInputStream(inputStream);
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
inputStream.close();
return bm;
} catch (IOException e) {
mThrown = e;
RestClient.consumeContent(mHttpResponse);
return null;
}
}
RestClient.consumeContent(mHttpResponse);
return null;
}
@Override
protected void onPostExecute(Bitmap bm) {
mImage.setImageBitmap(bm);
}
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.news_item, null);
TextView tvNewsTitle = (TextView) rowView.findViewById(R.id.tvNewsTitle);
TextView tvUpdateDate = (TextView) rowView.findViewById(R.id.tvUpdateDate);
ImageView ivImage = (ImageView) rowView.findViewById(R.id.ivNewsThumbnail);
News news = mNewsList.get(position);
tvNewsTitle.setText(news.getTitle());
tvUpdateDate.setText(news.getLastUpdate());
new LoadImageTask(ivImage, mContext).execute(news.getImageThumbnail());
return rowView;
}
しかし、常に ListView の最初の要素のみを更新しているようです。
サンプルコードはありますか?