Android中listview加载网络图片.doc
1.主Activity
public class MainActivity extends Activity {
private ListView listView;
private ArrayList persons;
private ListAdapter adapter;
private Handler handler=null;
//xml文件的网络地址
final String path="0: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=(ArrayList) msg.obj;
//给ListView绑定数据
BinderListData(person);
}
}
};
}
catch (Exception e)
{
e.printStackTrace();
}
}
//绑定数据
public void BinderListData(ArrayList person