android 75 新闻列表页面

new.xml

<?xml version="1.0" encoding="UTF-8" ?>
<newslist>
    <news>
        <title>黑马52期就业快报</title>
        <detail>热烈祝贺黑马52期平均薪水突破13k</detail>
        <comment>15687</comment>
        <image>http://192.168.1.100:8080/images/6.jpg</image>
    </news>
    <news>
        <title>程序员因写代码太乱被杀害</title>
        <detail>凶手是死者同事,维护死者代码时完全看不懂而痛下杀手</detail>
        <comment>16359</comment>
        <image>http://192.168.1.100:8080/images/7.jpg</image>
    </news>
    <news>
        <title>产品经理因频繁改需求被杀害</title>
        <detail>凶手是一名程序员,因死者对项目需求频繁改动而痛下杀手</detail>
        <comment>14112</comment>
        <image>http://192.168.1.100:8080/images/7.jpg</image>
    </news>
    <news>
        <title>3Q大战宣判: 腾讯获赔500万</title>
        <detail>最高法驳回360上诉, 维持一审宣判.</detail>
        <comment>6427</comment>
        <image>http://192.168.1.100:8080/images/1.jpg</image>
    </news>
    <news>
        <title>今日之声:北大雕塑被戴口罩</title>
        <detail>市民: 因雾霾起诉环保局; 公务员谈"紧日子": 坚决不出去.</detail>
        <comment>681</comment>
        <image>http://192.168.1.100:8080/images/2.jpg</image>
    </news>
    <news>
        <title>奥巴马见是装蒜</title>
        <detail>外文局: 国际民众认可中国大国地位;法院: "流量清零"未侵权.</detail>
        <comment>1359</comment>
        <image>http://192.168.1.100:8080/images/3.jpg</image>
    </news>
    <news>
        <title>轻松一刻: 我要沉迷学习不自拔</title>
        <detail>放假时我醒了不代表我起床了, 如今我起床了不代表我醒了!</detail>
        <comment>11616</comment>
        <image>http://192.168.1.100:8080/images/4.jpg</image>
    </news>
    <news>
        <title>男女那些事儿</title>
        <detail>"妈, 我在东莞被抓, 要2万保释金, 快汇钱到xxx!"</detail>
        <comment>10339</comment>
        <image>http://192.168.1.100:8080/images/5.jpg</image>
    </news>
    <news>
        <title>赵帅哥语录一</title>
        <detail>少壮不努力,老大做IT</detail>
        <comment>14612</comment>
        <image>http://192.168.1.100:8080/images/8.jpg</image>
    </news>
    <news>
        <title>赵帅哥语录二</title>
        <detail>问君能有几多愁,恰似调完代码改需求</detail>
        <comment>13230</comment>
        <image>http://192.168.1.100:8080/images/8.jpg</image>
    </news>
    <news>
        <title>赵帅哥语录三</title>
        <detail>觉得我帅的人工资一般都比较高</detail>
        <comment>9928</comment>
        <image>http://192.168.1.100:8080/images/8.jpg</image>
    </news>
    <news>
        <title>今日之声:北大雕塑被戴口罩</title>
        <detail>市民: 因雾霾起诉环保局; 公务员谈"紧日子": 坚决不出去.</detail>
        <comment>681</comment>
        <image>http://192.168.1.100:8080/images/2.jpg</image>
    </news>
    <news>
        <title>奥巴马见是装蒜</title>
        <detail>外文局: 国际民众认可中国大国地位;法院: "流量清零"未侵权.</detail>
        <comment>1359</comment>
        <image>http://192.168.1.100:8080/images/3.jpg</image>
    </news>
</newslist>

 主页面:

<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"
    tools:context=".MainActivity" >

    <ListView 
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ></ListView>

</RelativeLayout>

条目:

<?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="wrap_content" >
    
    <com.loopj.android.image.SmartImageView 
        android:id="@+id/iv"
        android:layout_width="90dp"
        android:layout_height="70dp"
        android:src="@drawable/ic_launcher"
        android:layout_centerVertical="true"        竖直居中
        />
    <TextView 
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"   和包裹内容一样,就是和里面的内容一样
        android:text="这是大标题志哥教你上塑料adasfsadfdsfdsgsd"
        android:layout_toRightOf="@id/iv"        右边
        android:textSize="22sp"
        android:singleLine="true"    标题不要换行,多余部分用...显示
        />
     <TextView 
         android:id="@+id/tv_detail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="这是正文志哥教你带崩三路adasfsadasdasdasdasidhsakjhdkjashdkjahskjdhsakjdfdsfdsgsd"
        android:layout_toRightOf="@id/iv"
        android:layout_below="@id/tv_title"
        android:textSize="15sp"
        android:textColor="@android:color/darker_gray"
        android:lines="2"            只能显示2行,多余用...显示        
        />
      <TextView 
          android:id="@+id/tv_comment"
        android:layout_width="wrap_content"    宽度为包裹内容
        android:layout_height="wrap_content"
        android:text="65031条评论"
        android:textColor="#ff0000"
        android:layout_alignParentRight="true"   位于右边
        android:layout_below="@id/tv_detail"
        />
</RelativeLayout>

Activity:

package com.itheima.xiaozhinews;

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.itheima.xiaozhinews.domain.News;
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.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

    List<News> newsList;
    Handler handler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            ListView lv = (ListView) findViewById(R.id.lv);
            lv.setAdapter(new MyAdapter());
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getNewsInfo();//下载新闻xml
//        ListView lv = (ListView) findViewById(R.id.lv);
//        //要保证在设置适配器时,新闻xml文件已经解析完毕了
//        lv.setAdapter(new MyAdapter());
    }

    class MyAdapter extends BaseAdapter{
        //得到模型层中元素的数量,用来确定listview需要有多少个条目
        @Override
        public int getCount() {//lv.setAdapter执行的时候就会调用getCount方法就会调用getView方法,
                            //所以要不保证lv.setAdapter执行的时候newsList已经初始化好了,不然就会有空指针,
                            //因此lv.setAdapter执行在子线程中的newsList初始化之后,
                            //由于lv.setAdapter是修改UI所以只能通过子线程发送消息给主线程进行UI修改
            return newsList.size();
        }
        @Override
        //返回一个View对象,作为listview的条目显示至界面
        public View getView(int position, View convertView, ViewGroup parent) {
            News news = newsList.get(position);
            View v = null;
            ViewHolder mHolder;
            if(convertView == null){
                v = View.inflate(MainActivity.this, R.layout.item_listview, null);
                mHolder = new ViewHolder();
                //把布局文件中所有组件的对象封装至ViewHolder对象中
                mHolder.tv_title = (TextView) v.findViewById(R.id.tv_title);
                mHolder.tv_detail = (TextView) v.findViewById(R.id.tv_detail);
                mHolder.tv_comment = (TextView) v.findViewById(R.id.tv_comment);
                mHolder.siv = (SmartImageView) v.findViewById(R.id.iv);
                //把ViewHolder对象封装至View对象中
                v.setTag(mHolder);
            }
            else{
                v = convertView;
                mHolder = (ViewHolder) v.getTag();
            }
            //利用缓存就不用执行findViewById
            mHolder.tv_title.setText(news.getTitle());
            mHolder.tv_detail.setText(news.getDetail());
            mHolder.tv_comment.setText(news.getComment() + "条评论");
            //给新闻图片imageview设置内容
            mHolder.siv.setImageUrl(news.getImageUrl());
            return v;//返回v所以缓存为View 
        }
        
        class ViewHolder{
            //条目的布局文件中有什么组件,这里就定义什么属性
            TextView tv_title;
            TextView tv_detail;
            TextView tv_comment;
            SmartImageView siv;
        }
        
        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }
    }
    private void getNewsInfo() {
        Thread t = new Thread(){
            @Override
            public void run() {
                String path = "http://192.168.13.13:8080/news.xml";
                try {
                    URL url = new URL(path);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    conn.setConnectTimeout(5000);//连接超时
                    conn.setReadTimeout(5000);//读取超时
                    //发送http GET请求,获取相应码
                    if(conn.getResponseCode() == 200){
                        InputStream is = conn.getInputStream();
                        //使用pull解析器,解析这个流
                        parseNewsXml(is);//之前是文件输入流,这里直接用服务端的流是一样的,
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        t.start();
    }
    
    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("image".equals(xp.getName())){
                        String image = xp.nextText();
                        news.setImageUrl(image);
                    }
                    break;
                case XmlPullParser.END_TAG:
                    if("news".equals(xp.getName())){
                        newsList.add(news);
                    }
                    break;
                }
                //解析完当前节点后,把指针移动至下一个节点,并返回它的事件类型
                type = xp.next();
            }
            //发消息,让主线程设置listview的适配器,如果消息不需要携带数据,可以发送空消息系统自动创建消息。
            handler.sendEmptyMessage(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

posted @ 2015-11-01 22:27  无天666  阅读(2270)  评论(0编辑  收藏  举报