1.定义类MapListImageAndText管理ListViewItem中控件的内容

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. 定义类MapListViewCache实例化ListViewItem中的控件

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. 定义类AsyncImageLoader,开启线程下载指定图片

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(, 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. 定义类MapListImageAndTextListAdapter继承ArrayAdapter

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, , 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.主程序中Listview与MapListImageAndTextListAdapter的捆绑

//tuangoupoints为对后台传回来的数据解析后得到的字符串
String[] mtuangoupoints =tuangoupoints.split("@"); List<MapListImageAndText> dataArray=new ArrayList<MapListImageAndText>(); for(int i=; i<mtuangoupoints.length;i++){
String[] tonepoint=mtuangoupoints[i].split("#"); String shopname=String.valueOf(i+)+tonepoint[];
String activityinfo=tonepoint[];
String address=tonepoint[];
String telephone=tonepoint[];
String imageurl=tonepoint[];
String distance=tonepoint[]; MapListImageAndText test=new MapListImageAndText(imageurl,shopname,activityinfo,address,telephone,distance);
dataArray.add(test);
} MapListImageAndTextListAdapter adapter=new MapListImageAndTextListAdapter(this, dataArray, mlistView);
mlistView.setAdapter(adapter);

Android Listview异步动态加载网络图片的更多相关文章

  1. Android高效异步图片加载框架

    概述 Android高效异步图片加载框架:一个高效的异步加载显示的图片加载框架,同时具备图片压缩,缓存机制等特性. 详细 代码下载:http://www.demodashi.com/demo/1214 ...

  2. Android中的动态加载机制

    在目前的软硬件环境下,Native App与Web App在用户体验上有着明显的优势,但在实际项目中有些会因为业务的频繁变更而频繁的升级客户端,造成较差的用户体验,而这也恰恰是Web App的优势.本 ...

  3. Android 实现布局动态加载

    Android 动态加载布局 通过使用LayoutInflater 每次点击按钮时候去读取布局文件,然后找到布局文件里面的各个VIEW 操作完VIEW 后加载进我们setContentView 方面里 ...

  4. android ListView上拉加载更多 下拉刷新功能实现(采用pull-to-refresh)

    Android实现上拉加载更多功能以及下拉刷新功能, 采用了目前比较火的PullToRefresh,他是目前实现比较好的下拉刷新的类库. 目前他支持的控件有:ListView, ExpandableL ...

  5. [Android Pro] so 动态加载—解决sdk过大问题

    原文地址: https://blog.csdn.net/Rong_L/article/details/75212472 前言 相信Android 开发中大家或多或少都会集成一些第三方sdk, 而其中难 ...

  6. Android学习——Fragment动态加载

    动态加载原理 利用FragmentManager来添加一套Fragment事务,最后通过commit提交该事务来执行对Fragment的相关操作. FragmentManager fragmentma ...

  7. 转: listview异步图片加载之优化篇(android)

    Listview异步加载之优化篇 关于listview的异步加载,网上其实很多示例了,总体思想差不多,不过很多版本或是有bug,或是有性能问题有待优化.有鉴于此,本人在网上找了个相对理想的版本并在此基 ...

  8. Android ListView避免多线程加载一个同一资源

    当我们的ListView中的Item包含图片,而且这些图片是同一资源,我们用多线程去加载图片,这时候可能就发生了这种情况. 比如线程是人,第一个人去做加载图片到缓存的工作,还没做好时第二个人要这同一张 ...

  9. PhotoSwipe异步动态加载图片

    在开发搜房家居M站的时候,搜房家居装修效果图相册展示效果需要用到PhotoSwipe插件来显示图片.特点:1. 家居提供的接口,每次只能获取一张图片2. 装修效果图的张数不限.3. 从PhotoSwi ...

随机推荐

  1. SQLite数据库安装与使用

    SQLite是遵守ACID的关系数据库管理系统,它包含在一个相对小的C库中.它是D.RichardHipp创建的公有领域项目. 不像常见的客户端/服务器结构范例,SQLite引擎不是个程序与之通信的独 ...

  2. 【转】android 开发 命名规范

    原文网址:http://www.cnblogs.com/ycxyyzw/p/4103284.html 标识符命名法标识符命名法最要有四种: 1 驼峰(Camel)命名法:又称小驼峰命名法,除首单词外, ...

  3. 【Latex】怎么写中文?

    最近总有这么几种情况:一.作业很简单,想用Latex敲,但是英语不过硬,用中文吧配中文环境就要配置半天.二.越来越多的朋友问我怎么搞中文输入,我也确实没啥帮助人家的好办法,所以只好自己研究研究怎么配置 ...

  4. 什么是 docker?

    关于 Docker 是什么,有个著名的隐喻:集装箱.但是它却起了个“码头工人”( docker 的英文翻译)的名字.这无疑给使用者很多暗示:“快来用吧!用了 Docker ,就像世界出现了集装箱,这样 ...

  5. Java并发实现一(并发的实现之Thread和Runnable的区别)

    package com.subject01; public class ThreadOrRunnable { public static void main(String[] args) { Syst ...

  6. Struts2简单例子

    Struts实现注册功能 ControlFilter.java package com.jikexueyuan.filter; import java.io.IOException; import j ...

  7. html.css随便记

    css 绝对定位:一个元素绝对定位时,浏览器首先将它从流中完全删除,然后浏览器再把这个元素放在属性指定的位置上,对其他元素没有影响   绝对定位要相对于最近的父级元素进行定位 position: ab ...

  8. web.xml配置DispatcherServlet

    1. org.springframework.web.servlet.DispatcherServlet 所在jar包: <dependency> <groupId>org.s ...

  9. Builder模式 初体验

        看来Java构造器模式,决定动手体验下.构造器模式是什么?干什么用的?推荐大家看下ITEYE的一篇文章     http://www.iteye.com/topic/71175     了解构 ...

  10. Oracle CheckPoint进程

    在实例经过分配内存结构,加载控制文件后,然后要打开数据库的时候,需要做到控制文件,数据文件,联机重做日志保持相互状态一致性,数据库才可以打开.当数据库发生实例不正常关闭时(比如系统掉电或者Shutdo ...