package com.org.demo.wangfeng;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import java.util.ArrayList;import java.util.List;import org.xmlpull.v1.XmlPullParser;import com.org.demo.wangfeng.demo.News;import com.org.wangfeng.R;import android.annotation.SuppressLint;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.util.Xml;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ListView;import android.widget.TextView;public class MainActivity extends Activity { // 下载的地址 private String path = ""; List<News> newsList; private ListView lv_main_list; @SuppressLint("HandlerLeak") Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { lv_main_list.setAdapter(new MyAdapter()); }; }; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); getNewInfo(); lv_main_list = (ListView) findViewById(R.id.lv_main_list); // 要保证在设置适配器时,新闻xml文件已经解析完毕 // lv_main_list.setAdapter(new MyAdapter()); }/** * 设置lv_main_list适配器 * @author Administrator * */ private class MyAdapter extends BaseAdapter { // 得到模型曾中元素的数量,用来确定ListView需要有多少个条目 @Override public int getCount() { // TODO Auto-generated method stub return newsList.size(); } // 返回一个View对象,作为ListView的条目显示至界面 @Override public View getView(int positon, View convertView, ViewGroup parent) { News newss = newsList.get(positon); View v; ViewHolder myHolder; if (convertView == null) { v = View.inflate(MainActivity.this, R.layout.mainlist_item, null); myHolder = new ViewHolder(); // 把布局文件中所有组件的对象封装至ViewHolder对象中 myHolder.tv_title = (TextView) v .findViewById(R.id.tv_main_title); myHolder.tv_detail = (TextView) v .findViewById(R.id.tv_main_detail); myHolder.tv_comment = (TextView) v .findViewById(R.id.tv_main_comment); // 把ViewHolder对象封装至View对象中 v.setTag(myHolder); } else { v = convertView; myHolder = (ViewHolder) v.getTag(); } // TODO Auto-generated method stub // 给三个文本框设置内容 myHolder.tv_title.setText(newss.getTitle()); myHolder.tv_detail.setText(newss.getDetail()); myHolder.tv_comment.setText(newss.getComment() + "条评论"); // 给新闻图片imageview设置内容 return v; } class ViewHolder { // 条目的布局文件中有什么组件,这里就定义什么组件 TextView tv_title; TextView tv_detail; TextView tv_comment; } @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; } } private void getNewInfo() { // TODO Auto-generated method stub Thread t = new Thread() { @Override public void run() { try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url .openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); conn.connect(); if (conn.getResponseCode() == 200) { // 服务器返回的流 InputStream is = conn.getInputStream(); // 使用pull解析器来解析流 parseNewsXml(is); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; t.start(); } /** 解析xml */ private void parseNewsXml(InputStream is) { XmlPullParser xp = Xml.newPullParser(); try { xp.setInput(is, "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())) { newsList = new ArrayList<News>(); } else if ("news".equals(xp.getName())) { news = new News(); } else if ("title".equals(xp.getName())) { String title = xp.nextText(); news.setTitle(title); } else if ("detail".equals(xp.getName())) { String detail = xp.nextText(); news.setDetail(detail); } else if ("comment".equals(xp.getName())) { String comment = xp.nextText(); news.setComment(comment); } else if ("imageUrl".equals(xp.getName())) { String imageUrl = xp.nextText(); news.setImageUrl(imageUrl); } break; case XmlPullParser.END_TAG: if ("news".equals(xp.getName())) { newsList.add(news); } break; } // 解析完当期的节点后,把指针移动至下个节点,并返回它的事件类型 type = xp.next(); } for (News i : newsList) { System.out.println(i.toString()); } // 发消息,让主线程设置ListView的适配器,如果消息不需要携带数据的话可以发送个空消息 handler.sendEmptyMessage(1);// 数据1 表示消息发送成功的意思 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
<?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" > <ImageView android:id="@+id/iv_main_image" android:layout_width="70dp" android:layout_height="85dp" android:contentDescription="@null" android:paddingBottom="2dp" android:paddingTop="3dp" android:scaleType="fitXY" android:src="@drawable/d" /> <TextView android:id="@+id/tv_main_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/iv_main_image" android:singleLine="true" android:text="这是大标题" android:textSize="23sp" /> <TextView android:id="@+id/tv_main_detail" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/iv_main_image" android:layout_toRightOf="@id/iv_main_image" android:lines="2" android:text="这是正文" android:textColor="@android:color/darker_gray" android:textSize="14sp" /> <TextView android:id="@+id/tv_main_comment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@id/tv_main_detail" android:text="评论书" android:textColor="#ff0000" /></RelativeLayout>