我就简介了一下,这里就不多说~
我实现了一个从服务器上解析XML文件的效果~
步骤如下:
1.把要解析的xml文件和要显示的图放在图片所示的位置上
2.编写布局文件
activity_main:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listview">
</ListView>
</RelativeLayout>
每一个条目的布局:list_item:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.loopj.android.image.SmartImageView
android:layout_width="90dp"
android:layout_height="70dp"
android:id="@+id/image"
android:layout_alignBottom="@+id/comment"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true">
</com.loopj.android.image.SmartImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_toRightOf="@id/image"
android:id="@+id/title"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/detail"
android:layout_toRightOf="@id/image"
android:layout_below="@id/title"
android:lines="2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/detail"
android:layout_alignParentRight="true"
android:id="@+id/comment"
android:textColor="#ff0000"
android:layout_marginRight="2dp"/>
</RelativeLayout>
在这个模具中使用了SmartImageView来快速的显示网络上的图片~
具体可以看我这篇:Android之使用SmartImageView加载图片
3.编写适配器,这里我写了万能适配器
具体万能适配器的写法:Android之listview万能适配器
我这里就不多说了
关键适配器代码:
public class MyAdapter extends CommonAdapter<News>{
public MyAdapter(Context context, List<News> data, int layoutId) {
super(context, data, layoutId);
// TODO Auto-generated constructor stub
}
@Override
public void current(ViewHolder holder, Object object) {
// TODO Auto-generated method stub
News news=(News) object;
((TextView)holder.getView(R.id.title)).setText(news.getTitle());
((TextView)holder.getView(R.id.detail)).setText(news.getDetail());
((TextView)holder.getView(R.id.comment)).setText(news.getComment());
((SmartImageView)holder.getView(R.id.image)).
setImageUrl(news.getImage());
}
}
4.编写MainActivity,这里就是使用PULL解析XML文件了~
public class MainActivity extends Activity {
private ListView listView;
private List<News>newslist;
private News news;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView) findViewById(R.id.listview);
getNews();
}
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
if(msg.what==0x123){
listView.setAdapter(new MyAdapter(MainActivity.this, newslist, R.layout.list_item));
}
}
};
//获取xml文件
private void getNews() {
// TODO Auto-generated method stub
new Thread(){
public void run() {
String path="http://192.168.102.2:8090/news.xml";
try {
URL url = new URL(path);
HttpURLConnection con=(HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setConnectTimeout(8000);
con.setReadTimeout(8000);
con.connect();
if(con.getResponseCode()==200){
InputStream is=con.getInputStream();
parseNewsxml(is);
handler.sendEmptyMessage(0x123);
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
};
}.start();
}
protected void parseNewsxml(InputStream is) {
// TODO Auto-generated method stub
XmlPullParser xp=Xml.newPullParser();
try {
xp.setInput(is,"UTF-8");
//对节点的时间类型判断,可以判断当前节点是什么节点
int type=xp.getEventType();
news=null;
while(type!=XmlResourceParser.END_DOCUMENT){
if(type==XmlResourceParser.START_DOCUMENT){
System.out.println("开始解析");
newslist=new ArrayList<News>();
}
if(type==XmlResourceParser.START_TAG){
if("news".equals(xp.getName())){
news=new News();
}
if("title".equals(xp.getName())){
String title=xp.nextText();
System.out.println("title:"+title);
news.setTitle(title);
}
if("detail".equals(xp.getName())){
String detail=xp.nextText();
System.out.println("detail:"+detail);
news.setDetail(detail);
}
if("comment".equals(xp.getName())){
String comment=xp.nextText();
System.out.println("comment:"+comment);
news.setComment(comment);
}
if("image".equals(xp.getName())){
String image=xp.nextText();
System.out.println("image:"+image);
news.setImage(image);
}
}
if(type==XmlResourceParser.END_TAG){
if("news".equals(xp.getName())){
newslist.add(news);
news=null;
}
}
type=xp.next();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我就不具体解说了~
嘻嘻
需要源码的这里:http://download.csdn.net/detail/qq_33642117/9586013