Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use Picasso with custom Adapter for RecyclerView

I am populating a RecyclerView with images that are loaded from web. I am able to load images using an AsyncTask inside my adapter. However, as I need to implement it with Picasso, I need a help. Here is the code so far:

    public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MovieViewHolder>
{
    Bitmap mBitmap;
    int pos;
    public static class MovieViewHolder extends RecyclerView.ViewHolder
    {
         CardView cv;
        TextView MovieName;
         ImageView MoviePhoto;

        MovieViewHolder(View itemView) {
            super(itemView);
            cv = (CardView)itemView.findViewById(R.id.cv);
            MovieName = (TextView)itemView.findViewById(R.id.movie_name);
            MoviePhoto = (ImageView)itemView.findViewById(R.id.movie_photo);
        }
    }

    List<Post> mpost;

    CustomAdapter(List<Post> mpost){
        this.mpost = mpost;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public MovieViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
        MovieViewHolder pvh = new MovieViewHolder(v);
        return pvh;
    }

    @Override
    public void onBindViewHolder(MovieViewHolder movieViewHolder, int i)
    {
        pos=i;
        movieViewHolder.MovieName.setText(mpost.get(i).getTitle());

        if(mpost.get(pos).getPoster_path()!=null)
        {
            new AsyncTask<Void, Void, Void>() {
                @Override
                protected Void doInBackground(Void... params) {
                    try {
                        URL url = new URL("http://image.tmdb.org/t/p/w154"+mpost.get(pos).getPoster_path());
                        mBitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());

                    } catch (MalformedURLException e) {

                    } catch (IOException e) {

                    }
                    return null;
                }
            }.execute();




            movieViewHolder.MoviePhoto.setImageBitmap(mBitmap);

        }




    }

    @Override
    public int getItemCount()
    {
        if(mpost!=null)
        {
            return mpost.size();
        }
        else
        {
            return 0;
        }
    }

}

And I need to replace this :

 new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                try {
                    URL url = new URL("http://image.tmdb.org/t/p/w154"+mpost.get(pos).getPoster_path());
                    mBitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());

                } catch (MalformedURLException e) {

                } catch (IOException e) {

                }
                return null;
            }
        }.execute();




        movieViewHolder.MoviePhoto.setImageBitmap(mBitmap);

with picasso:

Picasso.with(this)
                .load("http://image.tmdb.org/t/p/w154" + mpost.get(pos).getPoster_path())
                .into(MoviePhoto);

However, there seems to be error ddoing so, whats the best fix?

like image 996
OBX Avatar asked Oct 18 '22 19:10

OBX


1 Answers

@Override
public void onBindViewHolder(MovieViewHolder movieViewHolder, int i){
    pos=i;
    movieViewHolder.MovieName.setText(mpost.get(i).getTitle());
    if(mpost.get(pos).getPoster_path()!=null){
        Picasso.with(getContext()).load("http://image.tmdb.org/t/p/w154" + mpost.get(pos).getPoster_path()).into(movieViewHolder.MoviePhoto)
    }
}
like image 80
Deepak Avatar answered Nov 01 '22 16:11

Deepak