Search code examples
androidpicasso

Android using Picasso and TextDrawable in ListView


I have a ListView of user images. Some users have a profile picture and I use Picasso to load the images.
If user doesn't have an image, using TextDrawable I set his image in my listview adapter as shown below.

if (picture!=null) {
         Uri imageUri = Uri.parse(picture);
         Picasso.with(holder.pic.getContext()).load(imageUri.toString()).fit().centerCrop().into(holder.pic);

} else {
        ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT
        int color = generator.getColor(userName);
        String userChar = userName.substring(0, 1);
        TextDrawable drawable = TextDrawable.builder()
                .buildRound(userChar.toUpperCase(), color);
        holder.pic.setImageDrawable(drawable);

    }

Problem is when I scroll the list, sometimes the images mess up and those who don't have an image and are supposed to be displayed with TextDrawable are displayed with another users profile picture.
If I use Picasso to load all images this doesnt happen. Is there anyway to pass a TextDrawable object into picasso to solve this problem? Or is there any other solution?


Solution

  • You can use TextDrawable as drawable placeholder for Picasso. Then I think your problem would be resolved.

    ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT
    int color = generator.getColor(userName);
    String userChar = userName.substring(0, 1);
    TextDrawable drawable = TextDrawable.builder()
                .buildRound(userChar.toUpperCase(), color);
    Picasso.with(holder.pic.getContext()).
        load(imageUri.toString()).
        placeholder(drawable).
        error(drawable).
        centerCrop().
        into(holder.pic);