I'm loading an image from a server to a list view item using picasso like this:
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View participantView;
if(convertView == null) {
participantView = inflater.inflate(R.layout.participant_item, parent, false);
} else {
participantView = convertView;
}
TextView textView = (TextView) participantView.findViewById(R.id.participantName);
textView.setText(getItem(position).getName());
ImageView imageView = (ImageView) participantView.findViewById(R.id.participantImage);
String profilePic = getItem(position).getProfilePic();
if(!profilePic.equals("None")) {
Log.d("tom.debug", "creating picture for user: " + getItem(position).getName());
Picasso.with(this.context)
.load(urlToProfilePics + profilePic)
.placeholder(R.drawable.sample_0)
.resize(52, 52)
.into(imageView);
} else {
//load the place holder into the image view
Picasso.with(this.context).load(R.drawable.sample_0);
}
if(!getItem(position).isHere()) {
imageView.setColorFilter(Color.DKGRAY, PorterDuff.Mode.MULTIPLY);
}
return participantView;
}
The debug log under the if statement only fires for users that really have a profile picture. (Users that don't have will get a value of None
).
However, some of the other list view items (that don't have a profile pic) also get the picture loaded.
Another useful fact (I think): The items that get the bug changes when scrolling up and down the list.
I'm not sure what I'm missing here.
Image loading using Picasso is very easy, you can do it like this way Picasso. get(). load("http://i.imgur.com/DvpvklR.png").into(imageView); and in their website you can get every details. In your case you can parse every image URL and use RecyclerView to show them along with Picasso.
Picasso allows for hassle-free image loading in your application—often in one line of code! Picasso. get(). load("https://i.imgur.com/DvpvklR.png").into(imageView);
Make sure you call the cancelRequest everytime you are about to use Picasso on a getView() from the Adapter..
// 1st: reset the imageView
Picasso.with(this.context).cancelRequest(holder.imageView);
// 2nd start a new load for the imageView
Picasso.with(this.context).load(...).into(holder.imageView);
The reason is that the view you are reusing from the convertView parameter belongs to a previous row that was possibly already being loaded by Picasso for another picture.
This is only really necessary when using the convertView, if you have just inflated a new layout, it won't be needed..but you can call always to make your code easier.
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