listview1

Android Listview异步动态加载网络图片 
详见:  http://blog.sina.com.cn/s/blog_62186b460100zsvb.html
标签: Android SDK

代码片段(5)[全屏查看所有代码]

1. [代码](1)定义类MapListImageAndText管理ListViewItem中控件的内容     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.google.zxing.client.android.AsyncLoadImage;
 
 
 
public class MapListImageAndText {
         private String imageUrl;
         private String shopname;
         private String activitynifo;
         private String address;
         private String telephone;
         private String distance;
         
         public MapListImageAndText(String imageUrl, String shopname, String activitynifo, String address, String telephone,String distance) {
             this .imageUrl = imageUrl;
             this .shopname = shopname;
             this .activitynifo = activitynifo;
             this .address = address;
             this .telephone = telephone;
             this .distance=distance;
         }
 
         public String getImageUrl() {
             return imageUrl;
         }
         
         public String getShopname() {
             return shopname;
         }
 
         public String getActivitynifo() {
             return activitynifo;
         }
 
         public String getAddress() {
             return address;
         }
 
         public String getTelephone() {
             return telephone;
         }
         
         public String getDistance() {
             return distance;
         }
 
         
}

2. [代码](2)定义类MapListViewCache实例化ListViewItem中的控件     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.google.zxing.client.android.AsyncLoadImage;
 
import com.google.zxing.client.android.R;
 
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
 
public class MapListViewCache {
 
         private View baseView;
         private TextView shopname;
         private TextView activitynifo;
         private TextView address;
         private TextView telephone;
         private TextView distance;
 
         private ImageView imageView;
 
         public MapListViewCache(View baseView) {
             this .baseView = baseView;
         }
 
         public TextView getShopname() {
             if (shopname == null ) {
                 shopname = (TextView) baseView.findViewById(R.id.maplistviewitemshopname);
             }
             return shopname;
         }
         
         public TextView getActivitynifo() {
             if (activitynifo == null ) {
                 activitynifo = (TextView) baseView.findViewById(R.id.maplistviewitemActi);
             }
             return activitynifo;
         }
         
         public TextView getAddress() {
             if (address == null ) {
                 address = (TextView) baseView.findViewById(R.id.maplistviewitemaddr);
             }
             return address;
         }
         
         public TextView getTelephone() {
             if (telephone == null ) {
                 telephone = (TextView) baseView.findViewById(R.id.maplistviewitemtelphone);
             }
             return telephone;
         }
 
         public ImageView getImageView() {
             if (imageView == null ) {
                 imageView = (ImageView) baseView.findViewById(R.id.maplistviewitemImage);
             }
             return imageView;
         }
         
         public TextView getDistance() {
             if (distance == null ) {
                 distance = (TextView) baseView.findViewById(R.id.maplistviewitemdistance);
             }
             return distance;
         }
 
}

3. [代码](3)定义类AsyncImageLoader,开启线程下载指定图片     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.google.zxing.client.android.AsyncLoadImage;
 
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
 
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
 
public class AsyncImageLoader {
 
      private HashMap<String, SoftReference<Drawable>> imageCache;
       
          public AsyncImageLoader() {
              imageCache = new HashMap<String, SoftReference<Drawable>>();
          }
       
          public Drawable loadDrawable( final String imageUrl, final ImageCallback imageCallback) {
              if (imageCache.containsKey(imageUrl)) {
                  SoftReference<Drawable> softReference = imageCache.get(imageUrl);
                  Drawable drawable = softReference.get();
                  if (drawable != null ) {
                      return drawable;
                  }
              }
              final Handler handler = new Handler() {
                  public void handleMessage(Message message) {
                      imageCallback.imageLoaded((Drawable) message.obj, imageUrl);
                  }
              };
              new Thread() {
                  @Override
                  public void run() {
                      Drawable drawable = loadImageFromUrl(imageUrl);
                      imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
                      Message message = handler.obtainMessage( 0 , drawable);
                      handler.sendMessage(message);
                  }
              }.start();
              return null ;
          }
       
         public static Drawable loadImageFromUrl(String url) {
             URL m;
             InputStream i = null ;
             try {
                 m = new URL(url);
                 i = (InputStream) m.getContent();
             } catch (MalformedURLException e1) {
                 e1.printStackTrace();
             } catch (IOException e) {
                 e.printStackTrace();
             }
             Drawable d = Drawable.createFromStream(i, "src" );
             return d;
         }
       
          public interface ImageCallback {
              public void imageLoaded(Drawable imageDrawable, String imageUrl);
          }
 
}

4. [代码](4)定义类MapListImageAndTextListAdapter继承ArrayAdapter     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.google.zxing.client.android.AsyncLoadImage;
 
import java.util.List;
 
import com.google.zxing.client.android.R;
 
import com.google.zxing.client.android.AsyncLoadImage.AsyncImageLoader.ImageCallback;
 
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
 
public class MapListImageAndTextListAdapter extends ArrayAdapter<MapListImageAndText> {
 
         private ListView listView;
         private AsyncImageLoader asyncImageLoader;
 
         public MapListImageAndTextListAdapter(Activity activity, List<MapListImageAndText> imageAndTexts, ListView listView) {
             super (activity, 0 , imageAndTexts);
             this .listView = listView;
             asyncImageLoader = new AsyncImageLoader();
         }
 
         public View getView( int position, View convertView, ViewGroup parent) {
             Activity activity = (Activity) getContext();
 
             // Inflate the views from XML
             View rowView = convertView;
             MapListViewCache viewCache;
             if (rowView == null ) {
                 LayoutInflater inflater = activity.getLayoutInflater();
                 rowView = inflater.inflate(R.layout.maplistviewitem, null );
                 viewCache = new MapListViewCache(rowView);
                 rowView.setTag(viewCache);
             } else {
                 viewCache = (MapListViewCache) rowView.getTag();
             }
             MapListImageAndText imageAndText = getItem(position);
 
             // Load the image and set it on the ImageView
             String imageUrl = imageAndText.getImageUrl();
             ImageView imageView = viewCache.getImageView();
             imageView.setTag(imageUrl);
             Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl, new ImageCallback() {
                 
                 
                 public void imageLoaded(Drawable imageDrawable, String imageUrl) {
                     ImageView imageViewByTag = (ImageView) listView.findViewWithTag(imageUrl);
                     if (imageViewByTag != null ) {
                         imageViewByTag.setImageDrawable(imageDrawable);
                     }
                 }
             });
             if (cachedImage == null ) {
                 imageView.setImageResource(R.drawable.refresh);
             } else {
                 imageView.setImageDrawable(cachedImage);
             }
             // Set the text on the TextView
             TextView shopname = viewCache.getShopname();
             shopname.setText(imageAndText.getShopname());
             
             TextView activitynifo = viewCache.getActivitynifo();
             activitynifo.setText(imageAndText.getActivitynifo());
             
             TextView address = viewCache.getAddress();
             address.setText(imageAndText.getAddress());
             
             TextView telephone = viewCache.getTelephone();
             telephone.setText(imageAndText.getTelephone());
             
             TextView distance = viewCache.getDistance();
             distance.setText(imageAndText.getDistance());
             
             return rowView;
         }
 
}

5. [代码](5)主程序中Listview与MapListImageAndTextListAdapter的捆绑     跳至 [1] [2] [3] [4] [5] [全屏预览]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//tuangoupoints为对后台传回来的数据解析后得到的字符串
String[] mtuangoupoints =tuangoupoints.split( "@" );
 
List<MapListImageAndText> dataArray= new ArrayList<MapListImageAndText>();
     
for ( int i= 0 ; i<mtuangoupoints.length;i++){
     String[] tonepoint=mtuangoupoints[i].split( "#" );
     
     String shopname=String.valueOf(i+ 1 )+tonepoint[ 2 ];
     String activityinfo=tonepoint[ 1 ];
     String address=tonepoint[ 6 ];
     String telephone=tonepoint[ 7 ];
     String imageurl=tonepoint[ 8 ];
     String distance=tonepoint[ 5 ];
     
     MapListImageAndText test= new MapListImageAndText(imageurl,shopname,activityinfo,address,telephone,distance);
     dataArray.add(test);
}
     
MapListImageAndTextListAdapter adapter= new MapListImageAndTextListAdapter( this , dataArray, mlistView);
mlistView.setAdapter(adapter);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值