I develop a simple RSS reader and i want to show title and image of each post in a recycler view.
There is where i use Picasso to load images from an ArrayList :
public void onBindViewHolder(ViewHolder viewHolder, int i) {
RssItem item = rssItems.get(i);
Picasso.with(F.context).load(item.imageLink).into(viewHolder.postImage);
viewHolder.postTitle.setText(item.title);
viewHolder.postAuthor.setText(item.postWriter);
viewHolder.postDate.setText(item.pubDate);
}
but it doesn't work ! I test Picasso with a single url and it works correctly , but when set the image links in a array list, it doesn't work .
I had the same issue when I wanted to load an image from its URL with API in extended RecyclerView.Adapter and RecyclerView.ViewHolder.
First of all, you must check the URL might not be empty or null and then load it with Picasso.
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
RssItem item = rssItems.get(i);
if(item.imageLink!=null && !item.imageLink.isEmpty()) {
Picasso.with(F.context)
.load(item.imageLink)
.placeholder(R.drawable.default_placeholder)
.error(R.drawable.error_placeholder)
// To fit image into imageView
.fit()
// To prevent fade animation
.noFade()
.into(viewHolder.postImage);
} else {
viewHolder.postImage.setImageDrawable(ContextCompat.getDrawable(F.context,R.drawable.default_placeholder));
}
viewHolder.postTitle.setText(item.title);
viewHolder.postAuthor.setText(item.postWriter);
viewHolder.postDate.setText(item.pubDate);
}
At last you must be aware of viewHolder.postImage
and how it's found. It might be null or not finded view by id correctly.