我有一个android listView,我用图片填充。
我通过静默分页从服务器加载列表。
我使用异步下载任务下载图像和缓存层以保留它们。
每当我的适配器执行getView时,它都会将图像标记设置为其URL。
然后在我将drawable加载到图像视图之前,我检查它仍然是相关的(因为它是异步任务并且滚动可能已经移动并且该项目已经被回收)通过将下载的url与保存在其中的url进行比较。标签
但是我看到通常只看到默认图像,
我的逻辑缺少什么?
private final LruCache<String, Bitmap> imageCache = new LruCache<String, Bitmap>(
100);
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (position != 0
&& (PublicMacros.OFFERS_PAGE_SIZE / 2 == position)
&& ((position / PublicMacros.OFFERS_PAGE_SIZE + 1)
* PublicMacros.OFFERS_PAGE_SIZE + 1) > mOffersList
.size() && lastTriggeringPosition != position)
// ask next page
// maybe add loader
{
lastTriggeringPosition = position;
mOffersListActivity.getOffersFromServer(++pageNumber,
PublicMacros.OFFERS_PAGE_SIZE);
}
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mOffersListActivity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.offer_list_item, null);
} else {
// fetch existing data
}
initLayout(position, convertView);
setChangeableViews(convertView);
return convertView;
}
//called in setChangeableViews
private void setItemBgImage(View convertView) {
String imageUrl = getImageUrlFromOffer(convertView);
Bitmap bitmap = imageCache.get(imageUrl);
// anyhow set to default image
Drawable dr = mOffersListActivity.getResources().getDrawable(
R.drawable.lock_closed);
ImageView offerImage = ((ImageView) convertView
.findViewById(R.id.offerImage));
if (bitmap != null) {
dr = new BitmapDrawable(
// bitmap compression code
mOffersListActivity.getResources(), bitmap);
} else {
// check if already in the process of downloading
if (!downloadingImageUrls.contains(imageUrl)) {
downloadingImageUrls.add(imageUrl);
new DownloadImageAsyncTask2(offerImage).execute(imageUrl);
}
}
offerImage.setImageDrawable(dr);
offerImage.setTag(imageUrl);
}
public class DownloadImageAsyncTask2 extends
AsyncTask<String, Void, Bitmap> {
private final ImageView imageView;
private String imageUrl;
public DownloadImageAsyncTask2(ImageView imageView) {
this.imageView = imageView;
}
@Override
protected void onPreExecute() {
Log.i("DownloadImageAsyncTask", "Starting image download task...");
}
@Override
protected Bitmap doInBackground(String... params) {
imageUrl = params[0];
try {
return BitmapFactory.decodeStream((InputStream) new URL(
imageUrl).getContent());
} catch (IOException e) {
Log.e("DownloadImageAsyncTask", "Error reading bitmap" + e);
downloadingImageUrls.remove(imageUrl);
}
return null;
}
}
答案 0 :(得分:0)