I am having problem in filling an ImageView
in my ListView
.
The problem is that all the image gets filled in the same place (at the bottom-most list-item's imageview). I can see image getting loaded fast while they gets filled.
public class TheaterListAdapter extends ArrayAdapter<ConstantsTheaterList> implements IResult {
private ViewHolder viewHolder;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView==null){
convertView = LayoutInflater.from(mContext).inflate(R.layout.fragment_theater_list_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.imageView = (ImageView) convertView.findViewById(R.id.theater_list_item_image);
viewHolder.textViewLatitude = (TextView) convertView.findViewById(R.id.theater_list_item_latitude);
viewHolder.textViewLongitude = (TextView) convertView.findViewById(R.id.theater_list_item_longitude);
viewHolder.textViewVicinity = (TextView) convertView.findViewById(R.id.theater_list_item_vicinity);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
//Picasso.with(getContext()).load(uri.toString()).into(mImageView); [WORKS FINE]
// Using Volley to fetch image from the web.
new VolleyService(mContext, this).volleyImageRequest(uri.toString(), 100, 100);
String latitude = getItem(position).latitude;
String longitude = getItem(position).longitude;
String vicinity = getItem(position).vicinity;
viewHolder.textViewLatitude.setText(latitude);
viewHolder.textViewLongitude.setText(longitude);
viewHolder.textViewVicinity.setText(vicinity);
return convertView;
}
//These are the callbacks that I receive in my Volley implementation. Now, I fill the bitmap once they are available.
@Override
public void notifySuccess(Object response) {
Bitmap bitmap = (Bitmap) response;
viewHolder.imageView.setImageDrawable(bitmap);
}
@Override
public void nofifyError(Object volleyError) {
Log.e(TAG, (String)volleyError);
}
private static class ViewHolder{
ImageView imageView;
TextView textViewLatitude;
TextView textViewLongitude;
TextView textViewVicinity;
}
}
I believe this must be because of the views being recycled.
However, everything works fine when I use picasso.
How do I fix the same ?
For everyone out there, who would still like to understand why this doesn't work and would like to fix it that way, here is how to do it:
Everytime you call your method to fetch your image in background, you need to pass the instance of imageView too where this image is supposed to be placed.
Here is a sample where I've used
AsyncTask
to fetch the image.Now that you have understood, you can use Picasso for this thing. But it's important to understand why it didn't work in the first place.