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

运行结果如下

Android中ListView异步加载数据的更多相关文章

  1. Android中ListView异步加载图片错位、重复、闪烁问题分析及解决方案

    我们在使用ListView异步加载图片的时候,在快速滑动或者网络不好的情况下,会出现图片错位.重复.闪烁等问题,其实这些问题总结起来就是一个问题,我们需要对这些问题进行ListView的优化. 比如L ...

  2. Android中ListView分页加载数据

    public class MainActivity extends Activity { private ListView listView=null; //listview的数据填充器 privat ...

  3. Android中ListView动态加载数据

    1. 引言: 为了提高ListView的效率和应用程序的性能,在Android应用程序中不应该一次性加载ListView所要显示的全部信息,而是采取分批加载策略,随着用户的滑动,动态的从后台加载所需的 ...

  4. Android的ListView异步加载图片时,错位、重复、闪烁问题的分析及解决方法

    Android ListView异步加载图片错位.重复.闪烁分析以及解决方案,具体问题分析以及解决方案请看下文. 我们在使用ListView异步加载图片的时候,在快速滑动或者网络不好的情况下,会出现图 ...

  5. wemall app商城源码Android之ListView异步加载网络图片(优化缓存机制)

    wemall-mobile是基于WeMall的android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.本文分享wemall app商城源码Android之L ...

  6. Android 实现ListView异步加载图片

    ListView异步加载图片是非常实用的方法,凡是是要通过网络获取图片资源一般使用这种方法比较好,用户体验好,下面就说实现方法,先贴上主方法的代码: package cn.wangmeng.test; ...

  7. 新手教程:不写JS,在MIP页中实现异步加载数据

    从需求谈起:在 MIP 页中异步加载数据 MIP(移动网页加速器) 的 加速原理 除了靠谱的 MIP-Cache CDN 加速外,最值得一提的就是组件系统.所有 JS 交互都需要使用 MIP 组件实现 ...

  8. Android之ListView异步加载图片且仅显示可见子项中的图片

    折腾了好多天,遇到 N 多让人崩溃无语的问题,不过今天终于有些收获了,这是实验的第一版,有些混乱,下一步进行改造细分,先把代码记录在这儿吧. 网上查了很多资料,发现都千篇一律,抄来抄去,很多细节和完整 ...

  9. Android ListView异步加载数据

    1.主Activity public class MainActivity extends Activity { private ListView listView; private ArrayLis ...

随机推荐

  1. 模型类中 Parcelable 接口使用

    package com.exmyth.ui.model; import java.util.ArrayList; import java.util.List; public class Product ...

  2. shell脚本编写汇集

    一.替换文本: ##1 sed -i 's/disabled=true/disabled=false/' /etc/fdfs/storage.conf ##2 sed -i 's/base_path= ...

  3. Android 之 自定义标签 和 自定义组件

    1    自定义标签 这是我的模板项目目录     既然想像 android:text  那样使用自己的标签,那么首先得有标签. 在 res/values/ 下我新建了个 mm_tag.xml (切记 ...

  4. cache 的设计与实现--转载

    本文整理自一下两篇博客:http://my.oschina.net/ScottYang/blog/298727http://my.oschina.net/u/866190/blog/188712 Ca ...

  5. codevs 3693 数三角形

    /* n*m个点中选3个 再排除三点共线 共线分两类 1 在横线或者竖线上 m*C(n,3) n*C(m,3) 2 在对角线上 这个比较麻烦 以为对角线和矩阵是一一对应的 我们转化成求矩阵 并且保证有 ...

  6. jQuery回到顶部

    jquery回到顶部 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset=& ...

  7. maven项目java包名的路径问题

    maven项目中错误: 找不到或无法加载主类

  8. oracle 存储过程,函数和包

    创建存储过程: 语法:create [or replace] PROCEDURE 过程名(参数列表)  AS PLSQL子程序体: 调用 存储过程的方式 两种1.execute(exec)     - ...

  9. 学习OpenSeadragon之五(工具条toolbar与自定义按钮)

    OpenSeadragon简介:学习OpenSeadragon之一(一个显示多层图片的开源JS库) 一.工具条toolbar设置 OpenSeadragon为我们提供了现成的工具条toolBar,工具 ...

  10. 从mysql读取大量数据时的实践

    背景 程序启动时,从mysql读取所有的数据,在内存中建立数据结构.mysql表中至少有100w条记录.以后根据时间定期从mysql增量读取数据,刷新内存结构. 表结构为{uid, product, ...