Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView shows wrong and duplicates images

I have a ListView and 12 ImageViews in it.

Every ImageView has different image which is loading from url. Images are shuffled and sometimes duplicated either I scroll or not.

I tried 10 other ways to solve this problem but have not succeeded.

This is the code I download and show images:

private static class ViewHolder {
    ImageView imageViewPhoto;

    Bitmap photo;
    boolean isDownloading;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder viewHolder;

    if (convertView == null) {

        // ...classical view holder and other operations...

        if (!viewHolder.isDownloading) {

            viewHolder.isDownloading = true;

            IImageDownload downloadInterface = new IImageDownload() {

                @Override
                public void onError(VolleyError error, String url) {
                }

                @Override
                public void onDownloaded(Bitmap response, String url) {

                        viewHolder.photo = response;
                    notifyDataSetChanged();
                }
            };

            imageDownloader.downloadImage(dataList.get(position).getPhotoPath(), true, downloadInterface);

        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }



    if (viewHolder.photo != null) {
        viewHolder.imageViewPhoto.setImageBitmap(viewHolder.photo);
    } else {
        viewHolder.imageViewPhoto.setImageResource(R.drawable.gray_background);
    }

}

Thanks in advance for any ideas!

like image 555
JustWork Avatar asked Mar 19 '23 17:03

JustWork


1 Answers

Before:

imageDownloader.downloadImage(dataList.get(position).getPhotoPath(), true, downloadInterface);

Put:

viewHolder.photo.setImageBitmap(null);

This will reset the ImageView's bitmap, as it is being recycled and therefore keeping its image.

like image 71
jmcdonnell40 Avatar answered Mar 29 '23 13:03

jmcdonnell40