Android初学者之Listview异步加载网络图片并动态更新

Android初学者之Listview异步加载网络图片并动态更新,第1张

概述应用实例:解析后台返回的数据,把每条都显示在ListView中,包括活动图片、店名、活动详情、地址、电话和距离等。在布局文件中ListView的定义:<ListView android:id="@id/maplistview" andr

应用实例:解析后台返回的数据,把每条都显示在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,中定义活动图片、店名、活动详情、地址、电话和距离的布局:

<?xml version="1.0" enCoding="utf-8"?><relativeLayout androID:ID="@+ID/relativeLayout01" androID:layout_wIDth="fill_parent" xmlns: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中控件的内容

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(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)定义类MaplistimageAndTextlistadapter继承ArrayAdapter,用于创建AsyncImageLoader实例,并指定控件的内容:

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)主程序中ListvIEw与MaplistimageAndTextlistadapter的捆绑

//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);[/i]


总结

以上是内存溢出为你收集整理的Android初学者之Listview异步加载网络图片并动态更新全部内容,希望文章能够帮你解决Android初学者之Listview异步加载网络图片并动态更新所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1148785.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存