Android开发实现ListView异步加载数据的方法详解
程序员文章站
2023-01-22 09:22:08
本文实例讲述了android开发实现listview异步加载数据的方法。分享给大家供大家参考,具体如下:
1.主activity
public class ma...
本文实例讲述了android开发实现listview异步加载数据的方法。分享给大家供大家参考,具体如下:
1.主activity
public class mainactivity extends activity { private listview listview; private arraylist<person> persons; private listadapter adapter; private handler handler=null; //xml文件的网络地址 final string path="http://192.168.5.10:8080/fileserver/person.xml"; @suppresslint("handlerleak") protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); listview=(listview) super.findviewbyid(r.id.listview); //cache=new file(environment.getexternalstoragedirectory().getabsolutepath()+"/cache"); //开一条子线程加载网络数据 runnable runnable=new runnable() { public void run() { try { thread.sleep(2000); //xmlwebdata解析网络中xml中的数据 persons=xmlwebdata.getdata(path); //发送消息,并把persons结合对象传递过去 handler.sendmessage(handler.obtainmessage(0, persons)); } catch (interruptedexception e) { e.printstacktrace(); } } }; try { //开启线程 new thread(runnable).start(); //handler与线程之间的通信及数据处理 handler=new handler() { public void handlemessage(message msg) { if(msg.what==0) { //msg.obj是获取handler发送信息传来的数据 @suppresswarnings("unchecked") arraylist<person> person=(arraylist<person>) msg.obj; //给listview绑定数据 binderlistdata(person); } } }; } catch (exception e) { e.printstacktrace(); } } //绑定数据 public void binderlistdata(arraylist<person> person) { //创建adapter对象 adapter=new listviewadapter(r.layout.item,this,person); //将adapter绑定到listview中 listview.setadapter(adapter); } }
2.从网络中获取xml文件并解析数据
public class xmlwebdata { private static arraylist<person> persons=null; 6 public static arraylist<person> getdata(final string path) { try { url url=new url(path); person person=null; httpurlconnection conn=(httpurlconnection) url.openconnection(); conn.setrequestmethod("get"); conn.setconnecttimeout(5000); if(conn.getresponsecode()==200) { inputstream inputstream=conn.getinputstream(); xmlpullparser xml=xml.newpullparser(); xml.setinput(inputstream, "utf-8"); int event=xml.geteventtype(); while(event!=xmlpullparser.end_document) { switch (event) { //开始解析文档 case xmlpullparser.start_document: persons=new arraylist<person>(); break; case xmlpullparser.start_tag: string value=xml.getname(); if(value.equals("person")) {//person对象的初始化必须在这里初始化不然可能出现为null的现象 person=new person(); //获取属性值 person.setid(new integer(xml.getattributevalue(0))); } else if(value.equals("name")) { person.setname(xml.nexttext()); } else if(value.equals("sex")) { person.setsex(xml.nexttext()); } else if(value.equals("age")) { person.setage(new integer(xml.nexttext())); } else if(value.equals("path")) { person.setpath(xml.nexttext()); } break; case xmlpullparser.end_tag: if(xml.getname().equals("person")) { persons.add(person); system.out.println(person.getname());; person=null; } break; } //解析下一个对象 event=xml.next(); } return persons; } } catch (exception e) { e.printstacktrace(); } return null; } }
3.person对象类
public class person { private int id; private string name; private string sex; private string path; public string getpath() { return path; } public void setpath(string path) { this.path = path; } private int age; public int getid() { return id; } public void setid(int id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getsex() { return sex; } public void setsex(string sex) { this.sex = sex; } public int getage() { return age; } public void setage(int age) { this.age = age; } public person(){ } }
4.adapter数据适配器类
public class listviewadapter extends baseadapter implements listadapter { private arraylist<person> data; private int id; private context context; private layoutinflater inflater; public listviewadapter(int item, mainactivity mainactivity,arraylist<person> data) { this.data=data; this.context=mainactivity; this.id=item; inflater=layoutinflater.from(context); } @override public int getcount() { return data.size(); } @override public object getitem(int position) { return data.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view view, viewgroup arg2) { textview name=null; textview sex=null; textview age=null; imageview img=null; if(view==null) { view=inflater.inflate(id, null); name=(textview) view.findviewbyid(r.id.personname); sex=(textview) view.findviewbyid(r.id.personsex); age=(textview) view.findviewbyid(r.id.personage); img=(imageview) view.findviewbyid(r.id.personimage); //保存view对象到objectclass类中 view.settag(new objectclass(name,sex,age,img)); } else { //得到保存的对象 objectclass objectclass=(objectclass) view.gettag(); name=objectclass.name; sex=objectclass.sex; age=objectclass.age; img=objectclass.img; } person person=(person) data.get(position); //帮数据绑定到控件上 name.settext(person.getname().tostring()); sex.settext("性别:"+person.getsex().tostring()); age.settext("年龄:"+string.valueof(person.getage())); //加载图片资源 loadimage(img,person.getpath()); return view; } private void loadimage(imageview img, string path) { //异步加载图片资源 asynctaskimageload async=new asynctaskimageload(img); //执行异步加载,并把图片的路径传送过去 async.execute(path); } private final class objectclass { textview name=null; textview sex=null; textview age=null; imageview img=null; public objectclass(textview name, textview sex, textview age,imageview img) { this.name=name; this.sex=sex; this.age=age; this.img=img; } } }
5.异步加载图片类
public class asynctaskimageload extends asynctask<string, integer, bitmap> { private imageview image=null; public asynctaskimageload(imageview img) { image=img; } //运行在子线程中 protected bitmap doinbackground(string... params) { try { url url=new url(params[0]); httpurlconnection conn=(httpurlconnection) url.openconnection(); conn.setrequestmethod("post"); conn.setconnecttimeout(5000); if(conn.getresponsecode()==200) { inputstream input=conn.getinputstream(); bitmap map=bitmapfactory.decodestream(input); return map; } } catch (exception e) { e.printstacktrace(); } return null; } protected void onpostexecute(bitmap result) { if(image!=null && result!=null) { image.setimagebitmap(result); } super.onpostexecute(result); } }
6.网络中的person.xml文件内容为
<?xml version="1.0" encoding="utf-8"?> <persons> <person id="1"> <name>张三</name> <sex>男</sex> <age>25</age> <path>http://192.168.5.10:8080/fileserver/chengjisihan.jpg</path> </person> <person id="2"> <name>李斯</name> <sex>男</sex> <age>78</age> <path>http://192.168.5.10:8080/fileserver/laozi.jpg</path> </person> <person id="3"> <name>王五</name> <sex>男</sex> <age>22</age> <path>http://192.168.5.10:8080/fileserver/lilongji.jpg</path> </person> <person id="4"> <name>庞聪</name> <sex>男</sex> <age>31</age> <path>http://192.168.5.10:8080/fileserver/lishimin.jpg</path> </person> <person id="5"> <name>孙膑</name> <sex>男</sex> <age>48</age> <path>http://192.168.5.10:8080/fileserver/lisi.jpg</path> </person> <person id="6"> <name>孙武</name> <sex>男</sex> <age>58</age> <path>http://192.168.5.10:8080/fileserver/liyuan.jpg</path> </person> <person id="7"> <name>成吉思汗</name> <sex>男</sex> <age>40</age> <path>http://192.168.5.10:8080/fileserver/sunbiin.jpg</path> </person> <person id="8"> <name>李渊</name> <sex>男</sex> <age>36</age> <path>http://192.168.5.10:8080/fileserver/sunwu.jpg</path> </person> <person id="9"> <name>李隆基</name> <sex>男</sex> <age>32</age> <path>http://192.168.5.10:8080/fileserver/wangwu.jpg</path> </person> <person id="10"> <name>武则天</name> <sex>女</sex> <age>55</age> <path>http://192.168.5.10:8080/fileserver/wuzetian.jpg</path> </person> </persons> <?xml version="1.0" encoding="utf-8"?> <persons> <person id="1"> <name>张三</name> <sex>男</sex> <age>25</age> <path>http://192.168.5.10:8080/fileserver/chengjisihan.jpg</path> </person> <person id="2"> <name>李斯</name> <sex>男</sex> <age>78</age> <path>http://192.168.5.10:8080/fileserver/laozi.jpg</path> </person> <person id="3"> <name>王五</name> <sex>男</sex> <age>22</age> <path>http://192.168.5.10:8080/fileserver/lilongji.jpg</path> </person> <person id="4"> <name>庞聪</name> <sex>男</sex> <age>31</age> <path>http://192.168.5.10:8080/fileserver/lishimin.jpg</path> </person> <person id="5"> <name>孙膑</name> <sex>男</sex> <age>48</age> <path>http://192.168.5.10:8080/fileserver/lisi.jpg</path> </person> <person id="6"> <name>孙武</name> <sex>男</sex> <age>58</age> <path>http://192.168.5.10:8080/fileserver/liyuan.jpg</path> </person> <person id="7"> <name>成吉思汗</name> <sex>男</sex> <age>40</age> <path>http://192.168.5.10:8080/fileserver/sunbiin.jpg</path> </person> <person id="8"> <name>李渊</name> <sex>男</sex> <age>36</age> <path>http://192.168.5.10:8080/fileserver/sunwu.jpg</path> </person> <person id="9"> <name>李隆基</name> <sex>男</sex> <age>32</age> <path>http://192.168.5.10:8080/fileserver/wangwu.jpg</path> </person> <person id="10"> <name>武则天</name> <sex>女</sex> <age>55</age> <path>http://192.168.5.10:8080/fileserver/wuzetian.jpg</path> </person> </persons>
运行结果如下
更多关于android相关内容感兴趣的读者可查看本站专题:《android控件用法总结》、《android开发入门与进阶教程》、《android视图view技巧总结》、《android编程之activity操作技巧总结》、《android数据库操作技巧总结》及《android资源操作技巧汇总》
希望本文所述对大家android程序设计有所帮助。
上一篇: 珍珠菇的家常做法!还不快进来看看?