Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Picasso does not work with Recycler View in android

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 .

like image 218
Farzad Farazmand Avatar asked Feb 06 '15 11:02

Farzad Farazmand


People also ask

How many types of RecyclerView are there in Android?

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.

How does recycler work in Android?

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.


3 Answers

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()));
}
like image 132
LissF Avatar answered Nov 14 '22 23:11

LissF


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.

like image 40
KeyOne Avatar answered Nov 14 '22 22:11

KeyOne


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.

like image 44
Pradeep Sheoran Avatar answered Nov 14 '22 23:11

Pradeep Sheoran