navidjons
navidjons

Reputation: 501

android-ImageView in listview not showing image when scroll up and down

I'm getting images from the internet via universalImageDownloader and I'm using ViewHolder for showing them :

public class ListAdapter2 extends BaseAdapter {
    Activity activity;
    public ArrayList<HashMap<String, String>> list;

    public ListAdapter2(Activity activity, ArrayList<HashMap<String, String>> list) {
        super();
        this.activity = getActivity();
        this.list = list;
    }

    public HashMap<String, String> geting(int position) {
        return list.get(position);
    }

    public void addAll(ArrayList<HashMap<String, String>> result) {
        if (this.list == null) {
            this.list = result;
        } else {
            this.list.addAll(result);
        }
        notifyDataSetChanged();
    }

    public int getCount() {
        return list.size();
    }

    public Object getItem(int position) {
        return list.get(position);
    }

    public long getItemId(int arg0) {
        return 0;
    }

    private class ViewHolder {
        ImageView img;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        LayoutInflater inflater = activity.getLayoutInflater();
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.row_grids, null);
            holder = new ViewHolder();
            holder.img = (ImageView) convertView.findViewById(R.id.image);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        item = list.get(position);

        if(item.get("img").length()>0)
            imageLoader.displayImage("http://website.com/img/" + item.get("img"), holder.img, options,
                imageListener);
        else
            holder.img.setImageDrawable(getResources().getDrawable(R.drawable.fix));
        }

        return convertView;
    }

    public void clear() {
        if (this.list.size() > 0)
            this.list.clear();
    }
}

private static class ImageDisplayListener extends SimpleImageLoadingListener {

    static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

    @Override
    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
        if (loadedImage != null) {
            ImageView imageView = (ImageView) view;
            boolean firstDisplay = !displayedImages.contains(imageUri);
            if (firstDisplay) {
                FadeInBitmapDisplayer.animate(imageView, 500);
                displayedImages.add(imageUri);
            }
        }
    }
}

I've tested it with 3 phones and it works fine but when I test it on my friends't phone, some times one of the images is not showing .

For example, when I come to activity , it shows all the images and when I scroll down where last items are not showing anymore and then I scroll up, suddenly one of the images that's been showing before is not showing anymore and it shows the default image for it .

Is there anything wrong with this code ? By the way, I've 4 tabs and this happens in the tabs .

Thanks

Upvotes: 1

Views: 569

Answers (2)

Monu
Monu

Reputation: 41

use "lazyImageLoaderAdapter.notifyDataSetChanged();" where lazyImageLoaderAdapter is the object of any adapter.

Upvotes: 0

Abdul Rahman Majeed
Abdul Rahman Majeed

Reputation: 1135

the problem with the displayImage method, it could not encode URL properly some cases ..the better approch you should use Picasso library

if you are using android studio so add

compile 'com.squareup.picasso:picasso:2.5.2'

in build.gradle file and sync:

or else Picasso Site download Jar file for eclipse

after that you are able to use Picasso library and replace your lines with

if(item.get("img").length()>0)
        imageLoader.displayImage("http://website.com/img/" + item.get("img"), holder.img, options,
            imageListener);
    else
        holder.img.setImageDrawable(getResources().getDrawable(R.drawable.fix));
    }

 String imgUrl = "http://website.com/img/" + item.get("img");
    if (!imgUrl.equals(""))
        Picasso.with(activity).load(imgUrl).into(holder.img);
    else
        Picasso.with(activity).load(R.drawable.fix).into(holder.img );

Upvotes: 2

Related Questions