Android listview asynchronously loads Images

Source: Internet
Author: User
 package CN. riddles. activity; import android. app. activity; import android. OS. bundle; import android. widget. listview; public class mainactivity extends activity {private listview lv; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Lv = (listview) This. findviewbyid (R. id. test_lv); LV. setadapter (New songlistadapter (this) ;}
Package CN. riddles. activity; import android. content. context; import android. util. log; import android. view. layoutinflater; import android. view. view; import android. view. viewgroup; import android. widget. baseadapter; import android. widget. imageview; import android. widget. textview;/*** @ author riddlezhang song entry adapter */public class songlistadapter extends baseadapter {Private Static final string tag = "songlistadapter"; private context mcontext; private string [] strings = {"Wang Lihong", "Wu Zun", "He rundong", "Jin chengwu", "Wu yanzu"}; private string [] paths = {"http://list.image.baidu.com/t/image_category/galleryimg/menstar/hk/wang_li_hong.jpg ", "http://list.image.baidu.com/t/image_category/galleryimg/menstar/hk/wu_zun.jpg", "http://list.image.baidu.com/t/image_category/galleryimg/menstar/hk/he_run_dong.jpg", "http://list.image.baidu.com/t/image_category/galleryimg/menstar/hk/jin_cheng_wu.jpg", "http://list.image.baidu.com/t/image_category/galleryimg/menstar/hk/wu_yan_zu.jpg"}; Public songlistadapter (context mcontext) {This. mcontext = mcontext;} public void setmcontext (context mcontext) {This. mcontext = mcontext;} public int getcount () {return paths. length;} public object getitem (INT position) {return position;} public long getitemid (INT position) {return position;} public view getview (INT position, view convertview, viewgroup parent) {convertview = layoutinflater. from (mcontext ). inflate (R. layout. lv_adapter, null); imageview image = (imageview) convertview. findviewbyid (R. id. image); textview songer = (textview) convertview. findviewbyid (R. id. songer); image. settag (paths [position]); songer. settext (strings [position]); New canvasimagetask(.exe cute (image); // asynchronously load the Image Log. I (TAG, "execute:" + strings [position]); Return convertview ;}}
 
 
 
Package CN. riddles. activity; import Java. io. inputstream; import Java. lang. ref. softreference; import java.net. httpurlconnection; import java.net. URL; import Java. util. hashmap; import android. content. context; import android. graphics. drawable. drawable; import android. OS. asynctask; import android. util. log; import android. view. view; import android. webKit. urlutil;/*** @ author riddlezhang asynchronously loads images */public class Asyn Cviewtask extends asynctask <view, void, drawable> {private view mview; private hashmap <string, softreference <drawable> imagecache; Public asyncviewtask (context mcontext, string mobileinfo) {imagecache = new hashmap <string, softreference <drawable> ();} protected drawable doinbackground (view... views) {drawable = NULL; view = views [0]; If (view. gettag ()! = NULL) {If (imagecache. containskey (view. gettag () {softreference <drawable> cache = imagecache. get (view. gettag (). tostring (); drawable = cache. get (); If (drawable! = NULL) {return drawable;} Try {If (urlutil. ishttpurl (view. gettag (). tostring () {// if it is a network address. Then the connection URL downloads the image url = new URL (view. gettag (). tostring (); httpurlconnection conn = (httpurlconnection) URL. openconnection (); Conn. setdoinput (true); Conn. connect (); inputstream stream = Conn. getinputstream (); drawable = drawable. createfromstream (stream, "src"); stream. close ();} else {// if it is local data, parse drawable = drawable directly. createfrompath (view. gettag (). tostring () ;}} catch (exception e) {log. V ("IMG", E. getmessage (); Return NULL ;}} this. mview = view; return drawable;} protected void onpostexecute (drawable) {If (drawable! = NULL) {This. mview. setbackgrounddrawable (drawable); this. mview = NULL ;}}}

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.