Search code examples
androidlistviewadapterpicasso

Android ListView bug on first item using picasso


Ι have a ListView adapter that loads basketball team games. I'm loading my images with picasso or resources if it's the team that the app is for. On my first item it seems to load both teams's images from the web. Here is a sample code and image.

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

    View view = convertView;

    if (convertView == null) {
        view = LayoutInflater.from(context).inflate(layoutResource, parent, false);
    }

    Match programItem = getItem(position);     
    ImageView ourTeamLogo = (ImageView) view.findViewById(R.id.program_item_our_team_logo);      
    ImageView foeLogo = (ImageView) view.findViewById(R.id.program_item_foe_logo);

    if (programItem.isHome()) {

        ourTeamLogo.setImageResource(R.drawable.our_team_logo);

        Picasso.with(context).
                cancelRequest(foeLogo);
        Picasso.with(getContext())
                .load(programItem.getFoeLogoURL())
                .fit().placeholder(R.drawable.placeholder_team).into(foeLogo);        
    }
    else {

        foeLogo.setImageResource(R.drawable.our_team_logo);

        Picasso.with(context).
                cancelRequest(ourTeamLogo);
        Picasso.with(getContext())
                .load(programItem.getFoeLogoURL())
                .fit().placeholder(R.drawable.placeholder_team).into(ourTeamLogo);
    }

    return view;
}

See first item marked in red:

enter image description here


Solution

  • Found the solution, seems like I needed to add

    Picasso.with(context).cancelRequest(holder.ourTeamLogo); Picasso.with(context).cancelRequest(holder.foeTeamLogo);

        static class ViewHolder {
    ImageView ourTeamLogo, foeLogo;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final ViewHolder holder;
        if (convertView == null) {
        view = LayoutInflater.from(context).inflate(layoutResource, parent, false);
        holder = new ViewHolder();
        holder.ourTeamLogo = (ImageView) view.findViewById(R.id.program_item_our_team_logo);
        holder.foeLogo = (ImageView) view.findViewById(R.id.program_item_foe_logo);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    
    Match programItem = getItem(position);
    
    if (programItem.isHome()) {
    
        holder.ourTeamLogo.setImageResource(R.drawable.our_team_logo);
    
        Picasso.with(context).
                    cancelRequest(holder.ourTeamLogo);
        Picasso.with(getContext())
                .load(programItem.getFoeLogoURL())
                .fit().placeholder(R.drawable.placeholder_team).into(holder.foeLogo);
    } else {
    
        holder.foeLogo.setImageResource(R.drawable.our_team_logo);
        Picasso.with(context).
                    cancelRequest(holder.ourTeamLogo);
        Picasso.with(getContext())
                .load(programItem.getFoeLogoURL())
                .fit().placeholder(R.drawable.placeholder_team).into(holder.ourTeamLogo);
    }
    
    return view;
    

    }