package com.example.news;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import com.loopj.android.image.SmartImageView;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.util.Xml;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private List<News> list;
private ListView lv;
Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:
lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(new MyAdapter());
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//因为这里解析xml文件是在子线程里做的,而lv.setAdapter(new MyAdapter());是在主线程里做的
//如果主线程先执行的话,子线程xml还没有解析完,就会报空指针异常
//lv.setAdapter(new MyAdapter());
getInfo();
}
private class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int arg0, View view, ViewGroup arg2) {
News news = list.get(arg0);
ViewHolder myHolder;
View v=null;
if(view == null){
v = View.inflate(MainActivity.this, R.layout.item_lv, null);
myHolder = new ViewHolder();
myHolder.img = (SmartImageView) v.findViewById(R.id.img);
myHolder.tv_title = (TextView) v.findViewById(R.id.tv_title);
myHolder.tv_show = (TextView) v.findViewById(R.id.tv_show);
myHolder.tv_num = (TextView) v.findViewById(R.id.tv_num);
v.setTag(myHolder);
}else{
v = view;
myHolder = (ViewHolder) v.getTag();
}
myHolder.tv_title.setText(news.getTitle());
myHolder.tv_show.setText(news.getDetail());
myHolder.tv_num.setText(news.getComment()+"条评论");
myHolder.img.setImageUrl(news.getImage());
return v;
}
class ViewHolder{
SmartImageView img ;
TextView tv_title ;
TextView tv_show;
TextView tv_num;
}
}
private void getInfo() {
Thread t = new Thread() {
@Override
public void run() {
String path = "http://192.168.0.111:8080/images/news.xml";
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
if (conn.getResponseCode() == 200) {
InputStream in = conn.getInputStream();
//pull解析xml文件
parseNews(in);
} else {
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
t.start();
}
private void parseNews(InputStream in) {
XmlPullParser xp = Xml.newPullParser();
try {
// 初始化
xp.setInput(in, "utf-8");
// 返回节点类型
int type = xp.getEventType();
News news = null;
while (type != XmlPullParser.END_DOCUMENT) {
switch (type) {
case XmlPullParser.START_TAG:
if ("newslist".equals(xp.getName())) {
list = new ArrayList<News>();
} else if ("news".equals(xp.getName())) {
news = new News();
} else if ("title".equals(xp.getName())) {
news.setTitle(xp.nextText());
} else if ("detail".equals(xp.getName())) {
news.setDetail(xp.nextText());
} else if ("comment".equals(xp.getName())) {
news.setComment(xp.nextText());
} else if ("image".equals(xp.getName())) {
news.setImage(xp.nextText());
}
break;
case XmlPullParser.END_TAG:
if ("news".equals(xp.getName())) {
list.add(news);
}
break;
}
// 移动到下一个节点
type = xp.next();
}
// for (int i = 0; i < list.size(); i++) {
//
// System.out.println(list.get(i).toString());
// }
//必须得在解析之后才能给listview设置适配器
handler.sendEmptyMessage(1);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
-<newslist> -<news> <title>黑马52期就业快报</title> <detail>热烈祝贺黑马52期平均薪水突破13k</detail> <comment>15687</comment> <image>http://192.168.0.111:8080/images/6.jpg</image> </news> -<news> <title>程序员因写代码太乱被杀害</title> <detail>凶手是死者同事,维护死者代码时完全看不懂而痛下杀手</detail> <comment>16359</comment> <image>http://192.168.0.111:8080/images/7.jpg</image> </news> -<news> <title>产品经理因频繁改需求被杀害</title> <detail>凶手是一名程序员,因死者对项目需求频繁改动而痛下杀手</detail> <comment>14112</comment> <image>http://192.168.0.111:8080/images/7.jpg</image> </news> -<news> <title>3Q大战宣判: 腾讯获赔500万</title> <detail>最高法驳回360上诉, 维持一审宣判.</detail> <comment>6427</comment> <image>http://192.168.0.111:8080/images/1.jpg</image> </news> -<news> <title>今日之声:北大雕塑被戴口罩</title> <detail>市民: 因雾霾起诉环保局; 公务员谈"紧日子": 坚决不出去.</detail> <comment>681</comment> <image>http://192.168.0.111:8080/images/2.jpg</image> </news> -<news> <title>奥巴马见是装蒜</title> <detail>外文局: 国际民众认可中国大国地位;法院: "流量清零"未侵权.</detail> <comment>1359</comment> <image>http://192.168.0.111:8080/images/3.jpg</image> </news> -<news> <title>轻松一刻: 我要沉迷学习不自拔</title> <detail>放假时我醒了不代表我起床了, 如今我起床了不代表我醒了!</detail> <comment>11616</comment> <image>http://192.168.0.111:8080/images/4.jpg</image> </news> -<news> <title>男女那些事儿</title> <detail>"妈, 我在东莞被抓, 要2万保释金, 快汇钱到xxx!"</detail> <comment>10339</comment> <image>http://192.168.0.111:8080/images/5.jpg</image> </news> -<news> <title>赵帅哥语录一</title> <detail>少壮不努力,老大做IT</detail> <comment>14612</comment> <image>http://192.168.0.111:8080/images/8.jpg</image> </news> -<news> <title>赵帅哥语录二</title> <detail>问君能有几多愁,恰似调完代码改需求</detail> <comment>13230</comment> <image>http://192.168.0.111:8080/images/8.jpg</image> </news> -<news> <title>赵帅哥语录三</title> <detail>觉得我帅的人工资一般都比较高</detail> <comment>9928</comment> <image>http://192.168.0.111:8080/images/8.jpg</image> </news> -<news> <title>今日之声:北大雕塑被戴口罩</title> <detail>市民: 因雾霾起诉环保局; 公务员谈"紧日子": 坚决不出去.</detail> <comment>681</comment> <image>http://192.168.0.111:8080/images/2.jpg</image> </news> -<news> <title>奥巴马是装蒜</title> <detail>外文局: 国际民众认可中国大国地位;法院: "流量清零"未侵权.</detail> <comment>1359</comment> <image>http://192.168.0.111:8080/images/3.jpg</image> </news> </newslist>
package com.example.news;
public class News {
private String title;
private String detail;
private String comment;
private String image;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
@Override
public String toString() {
return "News [title=" + title + ", detail=" + detail + ", comment="
+ comment + ", image=" + image + "]";
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<com.loopj.android.image.SmartImageView
android:id="@+id/img"
android:layout_width="90dp"
android:layout_height="90dp"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="阿是讲道理费劲啊实例叠加飞拉萨的减肥蓝色"
android:textSize="25sp" />
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="2"
android:text="阿是讲道理费劲啊实例叠sfasfasdfasdasdfasdfasdfas加飞拉萨的减肥蓝色"
android:textColor="@android:color/darker_gray" />
<TextView
android:id="@+id/tv_num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="1000条评论"
android:textColor="#ff0000"
android:gravity="right"/>
</LinearLayout>
</LinearLayout>