欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现Listview异步加载网络图片并动态更新的方法

程序员文章站 2024-03-07 14:01:09
本文实例讲述了android实现listview异步加载网络图片并动态更新的方法。分享给大家供大家参考,具体如下: 应用实例:解析后台返回的数据,把每条都显示在listv...

本文实例讲述了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,中定义活动图片、店名、活动详情、地址、电话和距离的布局

<?xml version="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中控件的内容

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

更多关于android相关内容感兴趣的读者可查看本站专题:《android视图view技巧总结》、《android编程之activity操作技巧总结》、《android操作sqlite数据库技巧总结》、《android操作json格式数据技巧总结》、《android数据库操作技巧总结》、《android文件操作技巧汇总》、《android编程开发之sd卡操作方法汇总》、《android开发入门与进阶教程》、《android资源操作技巧汇总》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。