Android : Listview dynamically loaded images - First list items shows all images loaded in the listview and then clears out

on Friday, July 11, 2014


So I am dynamically loading images from a urls for each listview item.


Problem:


I seem to have a bug where on loading the listview, the first listview item's ImageView flashes through all the images for all listview items. After all of them load, then one by one the listview items show their images and the first one gets its proper image.


I am not sure what is going on and why is this happening.


Here is my image loading AsyncTask.


private class DownloadImageTask extends AsyncTask { private final WeakReference bmImage;



public DownloadImageTask(ImageView image) {
bmImage = new WeakReference(image);
}

protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}

protected void onPostExecute(Bitmap bitmap ) {
if (isCancelled()) {
bitmap = null;
}

if (bmImage != null) {
ImageView imageView = (ImageView)bmImage.get();

// if the imageView was not retrieved
if (imageView != null) {

// if the image source is not empty
if(bitmap != null){
imageView.setImageBitmap(bitmap);
}else{
// set placeholder image
imageView.setImageDrawable(imageView.getContext().getResources()
.getDrawable(R.drawable.social_eyes_bare_owl));
}
}
}
}
}

0 comments:

Post a Comment