Android实现Listview异步加载网络图片并动态更新的方法
本文实例讲述了Android实现Listview异步加载网络图片并动态更新的方法。分享给大家供大家参考,具体如下:
应用实例:解析后台返回的数据,把每条都显示在ListView中,包括活动图片、店名、活动详情、地址、电话和距离等。
在布局文件中ListView的定义:
<ListView android:id="@id/maplistview" android:background="@drawable/bg" android:layout_width="fill_parent" android:layout_height="fill_parent" android:cacheColorHint="#00000000" android:divider="@drawable/separator" android:dividerHeight="2.0px" android:layout_below="@id/mapseparator" />
在布局文件ListViewItem,中定义活动图片、店名、活动详情、地址、电话和距离的布局
<?xmlversion="1.0"encoding="utf-8"?> <RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:paddingBottom="2dip" android:paddingLeft="2dip" android:paddingRight="2dip"> <ImageView android:paddingTop="2dip" android:layout_alignParentLeft="true" android:layout_width="80px" android:layout_height="80px" android:id="@+id/maplistviewitemImage"/> <TextView android:layout_height="wrap_content" android:textSize="17dip" android:layout_width="fill_parent" android:id="@+id/maplistviewitemshopname" android:layout_toRightOf="@id/maplistviewitemImage" android:textColor="#000000"/> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_alignParentLeft="true" android:layout_below="@+id/maplistviewitemImage" android:id="@+id/maplistviewitemActi" android:textColor="#6C6C6C"/> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_alignParentLeft="true" android:layout_below="@+id/maplistviewitemActi" android:id="@+id/maplistviewitemaddr" android:textColor="#6C6C6C" android:singleLine="true"/> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_alignParentLeft="true" android:layout_below="@+id/maplistviewitemaddr" android:id="@+id/maplistviewitemtelphone" android:textColor="#6C6C6C" android:singleLine="true"/> </RelativeLayout>
(1)定义类MapListImageAndText管理ListViewItem中控件的内容
packagecom.google.zxing.client.android.AsyncLoadImage;
publicclassMapListImageAndText{
privateStringimageUrl;
privateStringshopname;
privateStringactivitynifo;
privateStringaddress;
privateStringtelephone;
privateStringdistance;
publicMapListImageAndText(StringimageUrl,Stringshopname,Stringactivitynifo,Stringaddress,Stringtelephone,Stringdistance){
this.imageUrl=imageUrl;
this.shopname=shopname;
this.activitynifo=activitynifo;
this.address=address;
this.telephone=telephone;
this.distance=distance;
}
publicStringgetImageUrl(){
returnimageUrl;
}
publicStringgetShopname(){
returnshopname;
}
publicStringgetActivitynifo(){
returnactivitynifo;
}
publicStringgetAddress(){
returnaddress;
}
publicStringgetTelephone(){
returntelephone;
}
publicStringgetDistance(){
returndistance;
}
}
(2)定义类MapListViewCache实例化ListViewItem中的控件
packagecom.google.zxing.client.android.AsyncLoadImage;
importcom.google.zxing.client.android.R;
importandroid.view.View;
importandroid.widget.ImageView;
importandroid.widget.TextView;
publicclassMapListViewCache{
privateViewbaseView;
privateTextViewshopname;
privateTextViewactivitynifo;
privateTextViewaddress;
privateTextViewtelephone;
privateTextViewdistance;
privateImageViewimageView;
publicMapListViewCache(ViewbaseView){
this.baseView=baseView;
}
publicTextViewgetShopname(){
if(shopname==null){
shopname=(TextView)baseView.findViewById(R.id.maplistviewitemshopname);
}
returnshopname;
}
publicTextViewgetActivitynifo(){
if(activitynifo==null){
activitynifo=(TextView)baseView.findViewById(R.id.maplistviewitemActi);
}
returnactivitynifo;
}
publicTextViewgetAddress(){
if(address==null){
address=(TextView)baseView.findViewById(R.id.maplistviewitemaddr);
}
returnaddress;
}
publicTextViewgetTelephone(){
if(telephone==null){
telephone=(TextView)baseView.findViewById(R.id.maplistviewitemtelphone);
}
returntelephone;
}
publicImageViewgetImageView(){
if(imageView==null){
imageView=(ImageView)baseView.findViewById(R.id.maplistviewitemImage);
}
returnimageView;
}
publicTextViewgetDistance(){
if(distance==null){
distance=(TextView)baseView.findViewById(R.id.maplistviewitemdistance);
}
returndistance;
}
}
(3)定义类AsyncImageLoader,开启线程下载指定图片
packagecom.google.zxing.client.android.AsyncLoadImage;
importjava.io.IOException;
importjava.io.InputStream;
importjava.lang.ref.SoftReference;
importjava.net.MalformedURLException;
importjava.net.URL;
importjava.util.HashMap;
importandroid.graphics.drawable.Drawable;
importandroid.os.Handler;
importandroid.os.Message;
publicclassAsyncImageLoader{
privateHashMap<String,SoftReference<Drawable>>imageCache;
publicAsyncImageLoader(){
imageCache=newHashMap<String,SoftReference<Drawable>>();
}
publicDrawableloadDrawable(finalStringimageUrl,finalImageCallbackimageCallback){
if(imageCache.containsKey(imageUrl)){
SoftReference<Drawable>softReference=imageCache.get(imageUrl);
Drawabledrawable=softReference.get();
if(drawable!=null){
returndrawable;
}
}
finalHandlerhandler=newHandler(){
publicvoidhandleMessage(Messagemessage){
imageCallback.imageLoaded((Drawable)message.obj,imageUrl);
}
};
newThread(){
@Override
publicvoidrun(){
Drawabledrawable=loadImageFromUrl(imageUrl);
imageCache.put(imageUrl,newSoftReference<Drawable>(drawable));
Messagemessage=handler.obtainMessage(0,drawable);
handler.sendMessage(message);
}
}.start();
returnnull;
}
publicstaticDrawableloadImageFromUrl(Stringurl){
URLm;
InputStreami=null;
try{
m=newURL(url);
i=(InputStream)m.getContent();
}catch(MalformedURLExceptione1){
e1.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
Drawabled=Drawable.createFromStream(i,"src");
returnd;
}
publicinterfaceImageCallback{
publicvoidimageLoaded(DrawableimageDrawable,StringimageUrl);
}
}
(4)定义类MapListImageAndTextListAdapter继承ArrayAdapter,用于创建AsyncImageLoader实例,并指定控件的内容
packagecom.google.zxing.client.android.AsyncLoadImage;
importjava.util.List;
importcom.google.zxing.client.android.R;
importcom.google.zxing.client.android.AsyncLoadImage.AsyncImageLoader.ImageCallback;
importandroid.app.Activity;
importandroid.graphics.drawable.Drawable;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.ArrayAdapter;
importandroid.widget.ImageView;
importandroid.widget.ListView;
importandroid.widget.TextView;
publicclassMapListImageAndTextListAdapterextendsArrayAdapter<MapListImageAndText>{
privateListViewlistView;
privateAsyncImageLoaderasyncImageLoader;
publicMapListImageAndTextListAdapter(Activityactivity,List<MapListImageAndText>imageAndTexts,ListViewlistView){
super(activity,0,imageAndTexts);
this.listView=listView;
asyncImageLoader=newAsyncImageLoader();
}
publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
Activityactivity=(Activity)getContext();
//InflatetheviewsfromXML
ViewrowView=convertView;
MapListViewCacheviewCache;
if(rowView==null){
LayoutInflaterinflater=activity.getLayoutInflater();
rowView=inflater.inflate(R.layout.maplistviewitem,null);
viewCache=newMapListViewCache(rowView);
rowView.setTag(viewCache);
}else{
viewCache=(MapListViewCache)rowView.getTag();
}
MapListImageAndTextimageAndText=getItem(position);
//LoadtheimageandsetitontheImageView
StringimageUrl=imageAndText.getImageUrl();
ImageViewimageView=viewCache.getImageView();
imageView.setTag(imageUrl);
DrawablecachedImage=asyncImageLoader.loadDrawable(imageUrl,newImageCallback(){
publicvoidimageLoaded(DrawableimageDrawable,StringimageUrl){
ImageViewimageViewByTag=(ImageView)listView.findViewWithTag(imageUrl);
if(imageViewByTag!=null){
imageViewByTag.setImageDrawable(imageDrawable);
}
}
});
if(cachedImage==null){
imageView.setImageResource(R.drawable.refresh);
}else{
imageView.setImageDrawable(cachedImage);
}
//SetthetextontheTextView
TextViewshopname=viewCache.getShopname();
shopname.setText(imageAndText.getShopname());
TextViewactivitynifo=viewCache.getActivitynifo();
activitynifo.setText(imageAndText.getActivitynifo());
TextViewaddress=viewCache.getAddress();
address.setText(imageAndText.getAddress());
TextViewtelephone=viewCache.getTelephone();
telephone.setText(imageAndText.getTelephone());
TextViewdistance=viewCache.getDistance();
distance.setText(imageAndText.getDistance());
returnrowView;
}
}
(5)主程序中Listview与MapListImageAndTextListAdapter的捆绑
//tuangoupoints为对后台传回来的数据解析后得到的字符串
String[]mtuangoupoints=tuangoupoints.split("@");
List<MapListImageAndText>dataArray=newArrayList<MapListImageAndText>();
for(inti=0;i<mtuangoupoints.length;i++){
String[]tonepoint=mtuangoupoints[i].split("#");
Stringshopname=String.valueOf(i+1)+tonepoint[2];
Stringactivityinfo=tonepoint[1];
Stringaddress=tonepoint[6];
Stringtelephone=tonepoint[7];
Stringimageurl=tonepoint[8];
Stringdistance=tonepoint[5];
MapListImageAndTexttest=newMapListImageAndText(imageurl,shopname,activityinfo,address,telephone,distance);
dataArray.add(test);
}
MapListImageAndTextListAdapteradapter=newMapListImageAndTextListAdapter(this,dataArray,mlistView);
mlistView.setAdapter(adapter);
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android视图View技巧总结》、《Android编程之activity操作技巧总结》、《Android操作SQLite数据库技巧总结》、《Android操作json格式数据技巧总结》、《Android数据库操作技巧总结》、《Android文件操作技巧汇总》、《Android编程开发之SD卡操作方法汇总》、《Android开发入门与进阶教程》、《Android资源操作技巧汇总》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。
热门推荐
7 简短的二胎祝福语
10 简短霸气女儿生日 祝福语
11 祝福语高考小众诗句简短
12 元旦祝福语20秒简短
13 老师过年祝福语大全 简短
14 一生祝福语简短
15 产检祝福语简短霸气
16 朋友店开张祝福语简短
17 生日爱情祝福语大全简短
18 朋友女儿生日祝福语 简短