3

I'm using volley library to load images from internet. I have a list view, some rows are loaded using volley's library "NetworkImageView" and others are loaded by a static resource.

It seems something is wrong, because I'm having rows with image wrongly assigned.

I have an ArrayAdapter, here you have my getView method:

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

    ViewHolder holder;

    House house = houses.get(position);

    if (convertView == null) {
        LayoutInflater inflater = ((Activity)getContext()).getLayoutInflater();
        convertView=inflater.inflate(this.layout, parent, false);

        holder=new ViewHolder();
        holder.image=(NetworkImageView) convertView.findViewById(R.id.imageView1);
        holder.name=(TextView) convertView.findViewById(R.id.housename_listelement_houseslist);
        holder.rating=(TextView) convertView.findViewById(R.id.houserating_listelement_houseslist);         

        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    if(house.getUrl()!=null){
        String url=house.getUrl();
        holder.image.setImageUrl(url, ((TopHousesListViewPager)getContext()).mImageLoader);
    }else{
        holder.image.setBackgroundResource(R.drawable.defaulthouse);
    }


    holder.name.setText(house.getName());
    holder.rating.setText(String.format("%.2f",house.getRate()));
return convertView;
}

As you can see, I'm only showing images from internet if my object has a url, but I'm haing rows where the object doesn't have url but the image is from other row...

Advices?

Thank you!

culebrins
  • 428
  • 9
  • 21

2 Answers2

10

As @Selvin said: You have to reset the image url each time after getting the holder:

    } else {
        holder = (SuggestionViewHolder) convertView.getTag();
        holder.image.setImageUrl(null, imageLoader);
    }

    if(house.getUrl()!=null){
       String url=house.getUrl();
       holder.image.setImageUrl(url, ((TopHousesListViewPager)getContext()).mImageLoader);
    } else {
       holder.image.setBackgroundResource(R.drawable.defaulthouse);
    } 

This worked for me.

muetzenflo
  • 5,653
  • 4
  • 41
  • 82
  • Hi All, I am also facing same issue and setting setIamger(null, mImageLoader) is not solving issue. I have also tried setimageBitmap(null) but that is also of no use. This issue is not occurring with picasso library. Has anyone solved this issue. – Manish Mar 14 '16 at 10:20
  • Any Idea how to solve this issue in recycler view Adapter ? – Antwan Apr 03 '16 at 08:15
3

In case anyone is using a more modern ViewHolder/getView paradigm with the fixed getTag and SparseArray in Android 4.0+, here is how you fix this problem

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

    if (convertView == null) {
        convertView = LayoutInflater.from(context)
                .inflate(R.layout.mLayout, parent, false);
    }

    );

            NetworkImageView mView = ViewHolder.get(convertView, R.id.mview);
           mView.setImageUrl(null, mImageLoader);

Set the imageUrl to null at this point, and then after you load your list object at the current position later in the getView, set the proper imageUrl there

CQM
  • 42,592
  • 75
  • 224
  • 366
  • Hi All, I am also facing same issue and setting setIamger(null, mImageLoader) is not solving issue. I have also tried setimageBitmap(null) but that is also of no use. This issue is not occurring with picasso library. Has anyone solved this issue. – Manish Mar 14 '16 at 10:19