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 .
Android RecyclerView Multiple ViewType Project Structure We'll be implementing three view types (text, image, audio) that are inflated by three different layouts. Each has its own implementation specified in the adapter class.
RecyclerView is the ViewGroup that contains the views corresponding to your data. It's a view itself, so you add RecyclerView into your layout the way you would add any other UI element. Each individual element in the list is defined by a view holder object.
Have you seen this article? It's about RecyclerView. Author uses Picasso in adapter.
@Override
public void onBindViewHolder(FeedListRowHolder feedListRowHolder, int i) {
FeedItem feedItem = feedItemList.get(i);
Picasso.with(mContext).load(feedItem.getThumbnail())
.error(R.drawable.placeholder)
.placeholder(R.drawable.placeholder)
.into(feedListRowHolder.thumbnail);
feedListRowHolder.title.setText(Html.fromHtml(feedItem.getTitle()));
}
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.
use just a simple code:
@Override
public void onBindViewHolder(ShivaViewholder holder, int position) {
Picasso.get().load("http://i.imgur.com/DvpvklR.png").into(holder.imageView2);
}
But if you also want the image in icon, or want the location then we will use some additional code in this code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With