gpt4 book ai didi

android - ListView 中的闪烁图像

转载 作者:行者123 更新时间:2023-11-29 01:32:45 25 4
gpt4 key购买 nike

<分区>

我正在尝试从 URL 将图像下载到 ListView 中。当我滚动或列表正在填充时,图像闪烁并且不在右行..

这是我的适配器代码:

public class RecipesAdapter extends ArrayAdapter<XmlDataCollected> {
// View lookup cache
private static class ViewHolder {
static ImageView img;
TextView name;
TextView info;
ImageDownloader controller;
}

public RecipesAdapter(Context context, List<XmlDataCollected> recipes) {
super(context, R.layout.fragment_search_result, recipes);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
XmlDataCollected recipe = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.fragment_search_result, parent, false);
viewHolder.name = (TextView) convertView.findViewById(R.id.label);
viewHolder.info = (TextView) convertView.findViewById(R.id.info);
ViewHolder.img = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
viewHolder.img.setImageBitmap(null);
}

if (viewHolder.controller != null) {
viewHolder.controller.cancel(false);
}

viewHolder.controller = new ImageDownloader(viewHolder.img);

if (viewHolder.controller != null) {
viewHolder.controller.execute(recipe.getImgUrl());
}

// Populate the data into the template view using the data object
viewHolder.name.setText(recipe.getRecipeName());
if(recipe.getType() != null) {
viewHolder.info.setText(recipe.getType());
}
else {
viewHolder.info.setText("Unknown category");
}

//new ImageDownloader(viewHolder.img).execute(recipe.getImgUrl());
// Return the completed view to render on screen
return convertView;
}

public class ImageDownloader extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;

public ImageDownloader(ImageView bmImage) {
this.bmImage = bmImage;
}

protected Bitmap doInBackground(String... urls) {
String url = urls[0];
Bitmap mIcon = null;
try {
InputStream in = new java.net.URL(url).openStream();
mIcon = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
}
return mIcon;

}

protected void onPostExecute(Bitmap result) {
if(result != null) {
bmImage.setImageBitmap(result);
}
else{
bmImage.setImageResource(R.drawable.ic_launcher);
}
}
}
}

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