Android Listview异步动态加载网络图片

详见: http://blog.sina.com.cn/s/blog_62186b460100zsvb.html

标签: Android SDK

代码片段(5)

[代码] (1)定义类MapListImageAndText管理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 }

[代码] (2)定义类MapListViewCache实例化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 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 }

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

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> imageCache;

17

18 public AsyncImageLoader() {

19 imageCache = new HashMap>();

20 }

21

22 public Drawable loadDrawable(final String imageUrl, final ImageCallback imageCallback) {

23 if (imageCache.containsKey(imageUrl)) {

24 SoftReference softReference = imageCache.get(imageUrl);

25 Drawable drawable = softReference.get();

26 if (drawable != null) {

27 return drawable;

28 }

29 }

30 final Handler handler = new Handler() {

31 public void handleMessage(Message message) {

32 imageCallback.imageLoaded((Drawable) message.obj, imageUrl);

33 }

34 };

35 new Thread() {

36 @Override

37 public void run() {

38 Drawable drawable = loadImageFromUrl(imageUrl);

39 imageCache.put(imageUrl, new SoftReference(drawable));

40 Message 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 }

[代码] (4)定义类MapListImageAndTextListAdapter继承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 {

20

21 private ListView listView;

22 private AsyncImageLoader asyncImageLoader;

23

24 public MapListImageAndTextListAdapter(Activity activity, List imageAndTexts, ListView 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 = (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 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 }

[代码] (5)主程序中Listview与MapListImageAndTextListAdapter的捆绑

01 //tuangoupoints为对后台传回来的数据解析后得到的字符串

02 String[] mtuangoupoints =tuangoupoints.split("@");

03

04 List dataArray=new ArrayList();

05

06 for(int i=0; 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

android listview动态加载网络图片不显示,Android Listview异步动态加载网络图片相关推荐

  1. android 悬浮窗口和主界面同时显示,Android 悬浮窗口(及解决6.0以上无法显示问题)...

    思路实现 通过WindowManager添加一个View,创建一个系统顶级的窗口,实现悬浮窗口的效果. 本篇思路,来源于郭霖大神的悬浮窗口教程. 大致介绍WindowManager 类 创建的对象: ...

  2. android 加载列表占位,使用Glide从URL加载占位符以在加载GIF时显示(Android)

    您必须将.thumbnail(url)中的URL传递为 .thumbnail(Glide .with(context) .load(Url) .asBitmap() 或者像这样: – Drawable ...

  3. android显示多个网络图片不显示,Android显示网络图片实例

    本文实例讲述了Android显示网络图片的方法,分享给大家供大家参考.具体方法如下: 一般来说,在Android中显示一张网络图片其实是非常简单的,下面就是一个非常简单的例子: 步骤1: ① 创建你的 ...

  4. Vue + Element 实现请求加载数据时显示动效( Loading 加载 )

    一.版本说明,参考 Element 官网 "vue": "^2.5.2", "axios": "^0.18.1", &q ...

  5. android 全局 窗口,学习笔记:WindowManager显示Android全局悬浮窗口

    我用Android手机装了个电商软件,抢购用.自己手机的状态栏不能显示秒级别的时间,只能精确到分钟.为了能准确的把握抢购时间,自己边学习边开发了一个时间显示悬浮窗. WindowManager 参考文 ...

  6. android通知的内容图标都不显示,android状态栏通知Notification如何设置为下拉不显示通知内容和图标...

    2013-09-05 回答 根据activity的生命周期,在activity不显示时,会执行onstop函数,所以你在onstop函数(按退出键除外)里面把notification放在通知栏里,再此 ...

  7. Android Glide加载网络图片不显示,但用网页打开又正常显示

    前言 最近做Demo比较多,之前在搭网络框架的时候遇到了图片加载的问题,因为我以前的框架中加载网络图片是没有问题,这次居然出问题,但是其实也不难解决吧. 异常问题 报错有两个 ① Failed to ...

  8. android listview左右滑动动画效果,Android基于ListView实现类似QQ空间的滚动翻页与滚动加载效果...

    本文实例讲述了Android基于ListView实现类似QQ空间的滚动翻页与滚动加载效果.分享给大家供大家参考,具体如下: 1. 滚动加载 listView.setOnScrollListener(n ...

  9. android百度地图覆盖物异步加载图片,Android 百度地图marker中图片不显示的解决方法(推荐)...

    目的: 根据提供的多个经纬度,显示所在地的marker样式,如下: 问题: 1.发现marker中在线加载的图片无法显示出来: 2.获取多个对象后,却只显示出了一个marker: 以下为官网实现方法: ...

最新文章

  1. 如何在存储过程中得到被调用存储过程的结果集
  2. 修改AspNetSqlMembershipProvider的密码规则
  3. MVVM架构~knockoutjs实现简单的购物车
  4. 第二篇:数据可视化 - 基本API
  5. python用random产生验证码,以及random的一些其他用法
  6. 2019山科计算机专业分数线,2019山东科技大学研究生分数线汇总(含2016-2019历年复试)...
  7. P2249 【深基13.例1】查找(AC) 2022.1.28
  8. Android背景透明的 Dialog
  9. matlab2012b破解版安装
  10. CC26xx(CortexM3) UARTs
  11. scratch编程一款节奏小游戏
  12. 阿里云域名购买和域名解析教程
  13. 生产企业全流程生产管控_如何通过创建流程使生产率提高10倍
  14. Python基础编程题
  15. Vendor Model的使用
  16. html的介绍及常用标签,吊打面试官系列!
  17. Day165/200 JS import * from 用法
  18. windows 7 下,如何统计某文件夹下 视频总时长
  19. Linux——》系统日志
  20. VUE报错__Avoid mutating a prop directly since the value will be overwritten whenever the parent

热门文章

  1. NSString的各种用法总结(创建、截取、判断比较、转化数据类型、拼接、替换、添加、追加、读取、写入、删去、改变)
  2. nodejs的启动方式
  3. qrcode.js 二维码生成器
  4. 修改输入框placeholder的默认样式
  5. 使用jQuery清空表单
  6. 最少步数----深搜
  7. Shiro的authc过滤器的执行流程
  8. 跨域资源共享CORS详解
  9. 部署项目的问题(一)—— vue工程打包上线样式错乱问题
  10. 监控工具之zabbix server3.4 部署配置