gpt4 book ai didi

android - 为什么有时会在 ListView 中加载错误的图像?

转载 作者:太空宇宙 更新时间:2023-11-03 13:53:40 25 4
gpt4 key购买 nike

我有一个 ListView ,它在每个单元格中异步加载图像。当我尝试缓慢向下滚动时(在加载当前 View 中的所有图像之后),它可以完美地工作。但是当我试图在加载它们之前向下滚动并向上滚动时,我遇到了这个问题。细胞开始显示与其不对应的图像。

我的 getView 方法如下所示:

public View getView(final int position, View convertView, ViewGroup parent) {

// TODO Auto-generated method stub

View rowView = null;

if(convertView == null) {
rowView = inflater.inflate(R.layout.list_posts_item, null);
final Holder holder=new Holder();
holder.tvTitle=(TextView) rowView.findViewById(R.id.tvTitleNamePost);
holder.ivPrimaryImage=(ImageView) rowView.findViewById(R.id.ivPrimaryImage);
holder.tvLocality=(TextView) rowView.findViewById(R.id.tvLocalityPosts);
holder.tvDateCreated=(TextView) rowView.findViewById(R.id.tvDateCreated);
rowView.setTag(holder);
}else {
rowView=convertView;
}

Holder holder = (Holder)rowView.getTag();
holder.ivPrimaryImage.setId(position);
holder.ivPrimaryImage.setTag(listOfPosts.get(position).getPostId());
holder.ivPrimaryImage.setImageBitmap(null); // Added for flickering issue
holder.tvTitle.setText(listOfPosts.get(position).getTitle());
holder.tvLocality.setText(listOfPosts.get(position).getLocality());
holder.tvDateCreated.setText(listOfPosts.get(position).getCreatedDate());
postId = listOfPosts.get(position).getPostId();
Image image = new Image();
image.setImg(holder.ivPrimaryImage);

if (!"N".equalsIgnoreCase(listOfPosts.get(position).getHasImage()) ) {
if(!tagsCaching.containsKey(postId))
new GetPrimaryImages().execute(image);
else
holder.ivPrimaryImage.setImageBitmap(tagsCaching.get(postId));
}

return rowView;
}

我的异步调用类如下所示:

public class GetPrimaryImages extends AsyncTask<Image, Void, Bitmap> {

ImageView imageView = null;
protected Bitmap doInBackground(Image... images) {
this.imageView=images[0].getImg();



// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("postid",(String)(this.imageView.getTag()) ));


json = jsonParser.makeHttpRequest(CommonResources.getURL("get_primary_image"),
"POST", params);


if(json == null){
return null;
}
Log.d("Fetching Image",imageView.getTag()+ json.toString());
tagsDownloaded.add((String)imageView.getTag());
// check for success tag
String TAG_SUCCESS = "success";
try {
int success = json.getInt(TAG_SUCCESS);

if (success == 0) {
image = json.getString("primaryimage");

}
} catch (JSONException e) {
e.printStackTrace();
}

return getImage(image);

}

/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(Bitmap result) {
tagsCaching.put((String)imageView.getTag(), result);
imageView.setImageBitmap(result);

}

public Bitmap getImage(String imageString) {
if("null".equalsIgnoreCase(imageString)){
return null;
}else{
byte[] decodedString = Base64.decode(imageString, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
//image.setImageBitmap(decodedByte);
return decodedByte;
}

}


}

编辑:

我向 Holder 添加了一个新的实例变量:

public class Holder
{
TextView tvTitle;
ImageView ivPrimaryImage;
TextView tvLocality;
TextView tvDateCreated;
int position;
}

在getView中设置相同: holder.position = 位置;并将 holder 对象传递给异步任务: new GetPrimaryImages(position, holder).execute(image);

并修改Async调用类如下:1.添加取消到http调用2.修改onPostExecute方法

  public class GetPrimaryImages extends AsyncTask<Image, Void, Bitmap> {

int mPosition;
Holder mHolder;
public GetPrimaryImages(int position, Holder holder){
mPosition = position;
mHolder = holder;
}

ImageView imageView = null;
protected Bitmap doInBackground(Image... images) {
this.imageView=images[0].getImg();



List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("postid",(String)(this.imageView.getTag()) ));


JSONObject json;
if(mHolder.position == mPosition)
json = jsonParser.makeHttpRequest(CommonResources.getURL("get_primary_image"),
"POST", params);

else {
json = null;
cancel(true);
}

// check log cat fro response
if(json == null){
return null;
}
Log.d("Fetching Image",imageView.getTag()+ json.toString());
tagsDownloaded.add((String)imageView.getTag());
// check for success tag
String TAG_SUCCESS = "success";
try {
int success = json.getInt(TAG_SUCCESS);

if (success == 0) {
image = json.getString("primaryimage");

}
} catch (JSONException e) {
e.printStackTrace();
}

return getImage(image);

}


protected void onPostExecute(Bitmap result) {
if (mHolder.position == mPosition) {
tagsCaching.put((String) imageView.getTag(), result);
imageView.setImageBitmap(result);
}

}

public Bitmap getImage(String imageString) {


//needs to wait
if("null".equalsIgnoreCase(imageString)){
return null;
}else{
byte[] decodedString = Base64.decode(imageString, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
//image.setImageBitmap(decodedByte);
return decodedByte;
}

}


}

这似乎是有效的。 :)

现在我怀疑缓存图像的最佳方式是什么?应该将其写入文件吗?每次向上滚动时都从中读取?

最佳答案

问题是,当您的异步任务结束其后台操作时,它链接到的元素已被回收以保存您集合中的另一个元素。

让我们关注元素位置,假设您的 ListView 最多可以显示 4 个元素。

listview 第一次为前 4 个元素调用 getview,并创建并运行四个 asynctasks。然后滚动到显示位置 11 - 15,第一个元素(与位置 1 相关的元素)在异步任务结束之前被回收到位置 11。

然后 asynctask 结束,你看到的是 post 11 相关的图像和 post 1 相关的位图。

避免这种情况的一种方法是在异步任务中知道 View 已被回收,正如 Lucas Rocha 的这篇旧帖子中所建议的那样。

Performance tips with listview

查看帖子以了解 ListView 如何工作:

enter image description here

关于android - 为什么有时会在 ListView 中加载错误的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32356029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com