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?
@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)
}
}
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