Android listview asynchronously and dynamically loads network images

Source: Internet
Author: User

Android listview asynchronously and dynamically loads network images
See: http://blog.sina.com.cn/s/blog_62186b460100zsvb.html
Tags: Android SDK
Code snippet (5)
[Code] (1) define the maplistimageandtext class to manage the contents of controls in listviewitem
01 package com. Google. zxing. Client. Android. asyncloadimage;
02
03
04
05 public class maplistimageandtext {
06 private string imageurl;
07 private string shopname;
08 private string activitynifo;
09 private string address;
10 private string telephone;
11 private string distance;
12
13 public maplistimageandtext (string imageurl, string shopname, string activitynifo, string address, string telephone, string distance ){
14 This. imageurl = imageurl;
15 this. shopname = shopname;
16 this. activitynifo = activitynifo;
17 this. Address = address;
18 this. Telephone = telephone;
19 this. Distance = distance;
20}
21
22 Public String getimageurl (){
23 return imageurl;
24}
25
26 Public String getshopname (){
27 return shopname;
28}
29
30 Public String getactivitynifo (){
31 return activitynifo;
32}
33
34 Public String getaddress (){
35 return address;
36}
37
38 public string gettelephone (){
39 return telephone;
40}
41
42 Public String getdistance (){
43 return distance;
44}
45
46
47}
[Code] (2) Definition class maplistviewcache instantiate controls in listviewitem
01 package com. Google. zxing. Client. Android. asyncloadimage;
02
03 import com. Google. zxing. Client. Android. R;
04
05 import Android. View. view;
06 import Android. widget. imageview;
07 import Android. widget. textview;
08
09 public class maplistviewcache {
10
11 private view baseview;
12 private textview shopname;
13 private textview activitynifo;
14 private textview address;
15 private textview telephone;
16 private textview distance;
17
18 private imageview;
19
20 public maplistviewcache (view baseview ){
21 This. baseview = baseview;
22}
23
24 public textview getshopname (){
25 if (shopname = NULL ){
26 shopname = (textview) baseview. findviewbyid (R. Id. maplistviewitemshopname );
27}
28 return shopname;
29}
30
31 public textview getactivitynifo (){
32 If (activitynifo = NULL ){
33 activitynifo = (textview) baseview. findviewbyid (R. Id. maplistviewitemacti );
34}
35 return activitynifo;
36}
37
38 public textview getaddress (){
39 if (address = NULL ){
40 address = (textview) baseview. findviewbyid (R. Id. maplistviewitemaddr );
41}
42 return address;
43}
44
45 public textview gettelephone (){
46 If (telephone = NULL ){
47 Telephone = (textview) baseview. findviewbyid (R. Id. maplistviewitemtelphone );
48}
49 return telephone;
50}
51
52 public imageview getimageview (){
53 If (imageview = NULL ){
54 imageview = (imageview) baseview. findviewbyid (R. Id. maplistviewitemimage );
55}
56 return imageview;
57}
58
59 Public textview getdistance (){
60 if (distance = NULL ){
61 distance = (textview) baseview. findviewbyid (R. Id. maplistviewitemdistance );
62}
63 return distance;
64}
65
66}
[Code] (3) define the class asyncimageloader and enable the thread to download the specified Image
01 package com. Google. zxing. Client. Android. asyncloadimage;
02
03 import java. Io. ioexception;
04 import java. Io. inputstream;
05 import java. Lang. Ref. softreference;
06 import java.net. malformedurlexception;
07 import java.net. url;
08 import java. util. hashmap;
09
10 Import Android. Graphics. drawable. drawable;
11 import Android. OS. Handler;
12 Import Android. OS. message;
13
14 public class asyncimageloader {
15
16 private hashmap <string, softreference <drawable> imagecache;
17
18 public asyncimageloader (){
19 imagecache = new hashmap <string, softreference <drawable> ();
20}
21
22 public drawable loaddrawable (final string imageurl, final imagecallback ){
23 if (imagecache. containskey (imageurl )){
24 softreference <drawable> softreference = imagecache. Get (imageurl );
25 drawable = softreference. Get ();
26 if (drawable! = NULL ){
27 return drawable;
28}
29}
30 final handler = new handler (){
31 public void handlemessage (message ){
32 imagecallback. imageloaded (drawable) message. OBJ, imageurl );
33}
34 };
35 new thread (){
36 @ override
37 public void run (){
38 drawable = loadimagefromurl (imageurl );
39 imagecache. Put (imageurl, new softreference <drawable> (drawable ));
40 message = handler. obtainmessage (0, drawable );
41 handler. sendmessage (Message );
42}
43}. Start ();
44 return NULL;
45}
46
47 Public static drawable loadimagefromurl (string URL ){
48 URL m;
49 inputstream I = NULL;
50 try {
51 m = new URL (URL );
52 I = (inputstream) M. getcontent ();
53} catch (malformedurlexception E1 ){
54 e1.printstacktrace ();
55} catch (ioexception e ){
56 E. printstacktrace ();
57}
58 drawable d = drawable. createfromstream (I, "src ");
59 return D;
60}
61
62 public interface imagecallback {
63 public void imageloaded (drawable imagedrawable, string imageurl );
64}
65
66}
[Code] (4) Definition class maplistimageandtextlistadapter inherits arrayadapter
01 package com. Google. zxing. Client. Android. asyncloadimage;
02
03 import java. util. List;
04
05 import com. Google. zxing. Client. Android. R;
06
07 import com. Google. zxing. Client. Android. asyncloadimage. asyncimageloader. imagecallback;
08
09 import Android. App. activity;
10 Import Android. Graphics. drawable. drawable;
11 import Android. View. layoutinflater;
12 Import Android. View. view;
13 Import Android. View. viewgroup;
14 Import Android. widget. arrayadapter;
15 Import Android. widget. imageview;
16 Import Android. widget. listview;
17 import Android. widget. textview;
18
19 public class maplistimageandtextlistadapter extends arrayadapter <maplistimageandtext> {
20
21 private listview;
22 private asyncimageloader;
23
24 public maplistimageandtextlistadapter (activity, list <maplistimageandtext> imageandtexts, listview ){
25 super (activity, 0, imageandtexts );
26 This. listview = listview;
27 asyncimageloader = new asyncimageloader ();
28}
29
30 public view getview (INT position, view convertview, viewgroup parent ){
31 activity = (activity) getcontext ();
32
33 // inflate the views from XML
34 view rowview = convertview;
35 maplistviewcache viewcache;
36 IF (rowview = NULL ){
37 layoutinflater Inflater = activity. getlayoutinflater ();
38 rowview = Inflater. Inflate (R. layout. maplistviewitem, null );
39 viewcache = new maplistviewcache (rowview );
40 rowview. settag (viewcache );
41} else {
42 viewcache = (maplistviewcache) rowview. gettag ();
43}
44 maplistimageandtext imageandtext = getitem (position );
45
46 // load the image and set it on the imageview
47 string imageurl = imageandtext. getimageurl ();
48 imageview = viewcache. getimageview ();
49 imageview. settag (imageurl );
50 drawable cachedimage = asyncimageloader. loaddrawable (imageurl, new imagecallback (){
51
52
53 public void imageloaded (drawable imagedrawable, string imageurl ){
54 imageview imageviewbytag = (imageview) listview. findviewwithtag (imageurl );
55 if (imageviewbytag! = NULL ){
56 imageviewbytag. setimagedrawable (imagedrawable );
57}
58}
59 });
60 if (cachedimage = NULL ){
61 imageview. setimageresource (R. drawable. Refresh );
62} else {
63 imageview. setimagedrawable (cachedimage );
64}
65 // set the text on the textview
66 textview shopname = viewcache. getshopname ();
67 shopname. settext (imageandtext. getshopname ());
68
69 textview activitynifo = viewcache. getactivitynifo ();
70 activitynifo. settext (imageandtext. getactivitynifo ());
71
72 textview address = viewcache. getaddress ();
73 address. settext (imageandtext. getaddress ());
74
75 textview telephone = viewcache. gettelephone ();
76 telephone. settext (imageandtext. gettelephone ());
77
78 textview distance = viewcache. getdistance ();
79 distance. settext (imageandtext. getdistance ());
80
81 return rowview;
82}
83
84}
[Code] (5) bundling of listview and maplistimageandtextlistadapter in the main program
01 // tuangoupoints is the string obtained after parsing the data transmitted from the background
02 string [] mtuangoupoints = tuangoupoints. Split ("@");
03
04 list <maplistimageandtext> dataarray = new arraylist <maplistimageandtext> ();
05
06 for (INT I = 0; I <mtuangoupoints. length; I ++ ){
07 string [] tonepoint = mtuangoupoints [I]. Split ("#");
08
09 string shopname = string. valueof (I + 1) + tonepoint [2];
10 string activityinfo = tonepoint [1];
11 string address = tonepoint [6];
12 string telephone = tonepoint [7];
13 string imageurl = tonepoint [8];
14 string distance = tonepoint [5];
15
16 maplistimageandtext test = new maplistimageandtext (imageurl, shopname, activityinfo, address, telephone, distance );
17 dataarray. Add (test );
18}
19
20 maplistimageandtextlistadapter adapter = new maplistimageandtextlistadapter (this, dataarray, mlistview );

21 mlistview. setadapter (adapter );

From: http://www.oschina.net/code/snippet_176897_7207

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.