Android ListView Async Load Issue

Source: Internet
Author: User

============ Problem Description ============


Learn to do Android, I want to imitate QQ space to do a small demo
The ListView encountered a problem when loading the picture asynchronously
Asynchronous loading using the SoftReference and file caching methods
Images are also found in memory or cache each time a picture is loaded
The first time it was loaded, the ListView slipped, and the same step came to Setimagebitmap.
But the picture doesn't appear to be blank.
Here are some pictures and code
Before sliding

After sliding
Image_url = new StringBuffer (Appconstant.download_image_url). Append (Msg.getopimagelist (). Get (0). Getimageurl ()). ToString (); ImageView ImageView = Holder.msgimage;imageview.settag (Image_url); Bitmap Bitmap = Loader.loadbitmap (ImageView, Image_url,new imagecallback () {@Overridepublic void Imageload (ImageView ImageView, Bitmap Bitmap) {if (Imageview.gettag ()! = null&& Imageview.gettag (). Equals (Image_url)) { Imageview.setimagebitmap (bitmap);}}); LOG.E ("Current Postion", "" "+ position), if (bitmap = = null) {imageview.setimagedrawable (Context.getresources (). getdrawable (R.drawable.ic_launcher));} else {imageview.setimagebitmap (bitmap);}

Loader
public class loader {/** *  memory picture Soft reference buffer  */private static HashMap< String, softreference<bitmap>> imagecache = null;public loader ()  { Imagecache = new hashmap<string, softreference<bitmap>> ();} Public static string getfilename (String url)  {return url.substring ( Url.lastindexof (File.separator)  + 1);} Public static bitmap getlocalresource (String destdir, string imagename)  { bitmap bmp = null; File imgedir = new file (DestDir); file cache = null;if  (!imgedir.exists ())  { //  determine if the local cache directory exists imgedir.mkdirs ();}  else {cache = new file (Destdir + file.separator + imagename);  //  determine if the picture resource exists if  (cache.exists ())  { //  bitmap object based on the picture's storage path if it exists  bmbmp  = bitmapfactory.decodefile(Cache.getabsolutepath ());}} Return bmp;} Public static bitmap loadbitmap (final imageview imageview, final string  imageurl,final imagecallback imagecallback)  {//  in the memory cache, returns the Bitmap object if  ( Imagecache.containskey (ImageURL))  {softreference<bitmap> reference = imagecache.get ( ImageURL); Bitmap bitmap = reference.get ();if  (Bitmap != null)  {return bitmap;}  else {/** *  Add a lookup  */string bitmapname = imageurl.substring to the local cache ( Imageurl.lastindexof ("/")  + 1); Bitmap bitmaptemp = null;bitmaptemp = getlocalresource (AppConstant.TEST,  Bitmapname);if  (Bitmaptemp != null)  {return bitmaptemp;}} Final handler handler = new handler ()  {/* *  (Non-javadoc)  *   *  @see  android.os.handler#handlemessage (android.os.Message)  */@Overridepublic  void handlemessage (message msg)  {// todo auto-generated  method stubimagecallback.imageload (imageview,  (Bitmap)  msg.obj);}};/ /  if it is not in the memory cache and is not local (reclaimed by the JVM), turn on thread download Picture new thread ()  {/* *  (Non-javadoc)  *   *  @see  java.lang.thread#run ()  */@Overridepublic  void run ()  {// todo  auto-generated method stubbitmap bitmap = null;try {url imageurl  = new url (ImageURL); httpurlconnection conn =   (HttpURLConnection)  imageurl.openconnection (); Conn.setconnecttimeout (30000); Conn.setreadtimeout (30000); Conn.setinstancefollowredirects (True);//  Inputstream is = conn.getinputstream (); Inputstream in = conn.getinputstream (); Bitmapfactory.options options = new bitmapfactory.options (); options.inJustDecodeBounds  = false;options.insamplEsize = 10; // width,hight is set to the original very bitmap = bitmapfactory.decodestream (in,  null, options);}  catch  (MALFORMEDURLEXCEPTION&NBSP;E1)  {// TODO Auto-generated catch  Blocke1.printstacktrace ();}  catch  (IOEXCEPTION&NBSP;E1)  {// TODO Auto-generated catch  Blocke1.printstacktrace ();} Imagecache.put (imageurl, new softreference<bitmap> (Bitmap)); Message msg = handler.obtainmessage (0, bitmap); Handler.sendmessage (msg); File dir = new file (appconstant.test);if  (!dir.exists ())  {dir.mkdirs (); File bitmapfile = new file (appconstant.test + file.separator+  Imageurl.substring (Imageurl.lastindexof ("/")  + 1);if  (!bitmapfile.exists ())  {try { Bitmapfile.createnewfile ();}  catch  (ioexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace();}} Fileoutputstream fos;try {fos = new fileoutputstream (BitmapFile); Bitmap.compress ( Bitmap.compressformat.jpeg, 100, fos); Fos.close ();}  catch  (filenotfoundexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}  catch  (ioexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}}}. Start (); return null;} /** *  Callback Interface  *  *  @author  onerain *  */public interface  imagecallback {public void imageload (Imageview imageview, bitmap bitmap);}}

Wait for the great God's weapon.
Every time I get into the setimagebitmap this step is to not load the picture

============ Solution 1============


You try this three-party open Source Library with Imageloader.

============ Solution 2============


Take a look at your ImageView control is set by SRC or background. Or try turning the bitmap into drawable, and then SetBackground try.

============ Solution 3============


Look at the GetView method?

============ Solution 4============


Put down the adapter code and see.

============ Solution 5============


You try asyncimage This third-party open Source Library

============ Solution 6============


ImageView Imageviewbytag = (ImageView) listview
. Findviewwithtag (IMAGEURL);

The question should be in this sentence. Your search will only find the first tag that is a ImageUrl control. It may not be your control that you want to update.

For example, you settag.the ListView will reuse the item,This tag is still in msg.getopimagelist (). Size () =0.
Then you find the result is the contentview inside, not you want to update the ImageView.

============ Solution 7============


References
Image_url = new StringBuffer (appconstant.download_image_url)
. Append (Msg.getopimagelist (). Get (0). Getimageurl ())
. toString ();

ImageView ImageView = holder.msgimage;
Imageview.settag (Image_url);
Bitmap Bitmap = Loader.loadbitmap (ImageView, Image_url,
New Imagecallback () {
@Override
public void Imageload (ImageView ImageView, Bitmap Bitmap) {
if (imageview.gettag () = null
&& Imageview.gettag (). Equals (Image_url)) {
Imageview.setimagebitmap (bitmap);
}
}
});
LOG.E ("Current Postion", "" "+ position);
if (bitmap = = null) {
Imageview.setimagedrawable (Context.getresources (). Getdrawable (
R.drawable.ic_launcher));
} else {
Imageview.setimagebitmap (bitmap);
}



This code in the Imagecallback interface bar ImageUrl back, with Imageview.gettag () to compare, should be able to solve the problem. Try.

Android ListView Async Load Issue

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.