我有一个Lazy适配器,可以将标题,副标题和图片添加到ListView。最近我在ListView中注意到我的图像被复制了。
这是我将数组添加到适配器
的地方protected void onPostExecute(String result) {
LazyAdapter adapter = new LazyAdapter(Services.this,menuItems);
serviceList.setAdapter(adapter);
}
这里我调用异步方法来添加我的图像(它的url就是为什么它在异步方法中)
vi.findViewById(R.id.thumbnail).setVisibility(View.VISIBLE);
title.setText(items.get("title")); // Set Title
listData.setText(items.get("htmlcontent")); // Set content
thumb_image.setBackgroundResource(R.layout.serviceborder);//Adds border
new setLogoImage(thumb_image).execute(); // Async Method call
这是我设置位图图像的地方
private class setLogoImage extends AsyncTask<Object, Void, Bitmap> {
private ImageView imv;
public setLogoImage(ImageView imv) {
this.imv = imv;
}
@Override
protected Bitmap doInBackground(Object... params) {
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream((InputStream)new URL(items.get("thumburl")).getContent());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
imv.setImageBitmap(Bitmap.createScaledBitmap(result, 50, 50, false));
}
}
items
变量是存储我的信息的HashMap。我获取给适配器的数据的位置,并将其分配给项HashMap。像这样:
items = new HashMap<String, String>();
items = data.get(position);
这似乎是随机发生的,并且在大约30%的时间内看到错误的图片会很烦人,特别是在我尝试调试的时候。
任何帮助都会很棒。感谢
这是一张图片,可以看到发生了什么
答案 0 :(得分:1)
任何时候你有随机发生的事情,并且你有任何一种线索,你应该想到两个作品,种族条件。基本上,您的AsyncTasks都加载相同的图像。它们应该传递给值来加载。我注意到你没有对params做任何事情。我不确定你应该做什么,但问题出在你的AsyncTask中。
@Override
protected Bitmap doInBackground(Object... params) {
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream((InputStream)new URL(items.get("thumburl")).getContent());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
您应该传入URL(或它代表的字符串)。在这个例子中,我传入了URL,但随意改变它你想要的方式。关键是,您应该将图像位置的表示传递给函数,而不是试图找出在函数中使用的图像。
new setLogoImage(new Url(items.get("thumburl"))).execute(); // Async Method call
@Override
protected Bitmap doInBackground(Url... input) {
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream((InputStream)input[0]).getContent());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}