7

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 .

5
  • Make sure that rssItems contains valid items, i.e. their urls are non-empty and the images can be loaded, for example, via web-browser. Commented Feb 6, 2015 at 11:34
  • i checked that , there are valid addresses in rssItems and all of them are working,when i use Picasso with one of the addresses from rssItem ( Like :Picasso.with(F.context).load("example.com").into(viewHolder.postImage); ) it works, but when i set item.imageLink , it doesn't work. Commented Feb 6, 2015 at 11:49
  • Please explain completely and precisely what "it doesn't work" means. Commented Feb 6, 2015 at 12:23
  • means picasso doesn't download image ! Commented Feb 6, 2015 at 12:27
  • check this tutorial to see how to load images in recyclerview using picasso library Commented Jan 6, 2019 at 7:53

6 Answers 6

8

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())); } 
Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

2

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.

Comments

1

I was stuck with this thing for some time but got it sorted after reading a resource online. This is what you need to do which worked for me. I will just solve your problem of loading image in imageview which you have declared in your ViewHolder class.

public void onBindViewHolder(ViewHolder viewHolder, int i) { Context context = viewHolder.postImage.getContext(); //<----- Add this line RssItem item = rssItems.get(i); //change context here Picasso.with(context).load(item.imageLink).into(viewHolder.postImage); viewHolder.postTitle.setText(item.title); viewHolder.postAuthor.setText(item.postWriter); viewHolder.postDate.setText(item.pubDate); } 

This is what worked for me and I hope it would work for you too. Good Luck.

Comments

1

Just a note you need to have this setup on your android manifest to get Picasso to load any image via url. Update to the answer:

For Picasso to load images from urls eg. https://picsum.photos/900/900?image=0. It needs to have the right permissions , which you need to set in your android manifest file....So in your java you will have :

Picasso.get().load("http://i.imgur.com/DvpvklR.png").into(imageView); 

but this might not load any images and seem as if Picasso is not working..so you have to update the AndroidManifest.xml and just below the manifest tag you add the following...:

<uses-permission android:name="android.permission.INTERNET" /> 

2 Comments

This needs more information for "the average person" to understand it.
updated the answer with more detail..as for me it was just a matter of updating the permissions. to get Picasso to work.
0

If you're loading images from internet - make sure that you have added permission for INTERNET in your AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET"/> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.