Android 学习之路 新闻客户端小案例(齐全哦)

这是一个效仿网易新闻客户端一个小页面的案例,通过此案例落实前期所学:

直接贴上代码成果

MainActivity
package com.zhongkechuangda.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.loopj.android.image.SmartImageView;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;


public class MainActivity extends AppCompatActivity {

    private List newsList;
    private TextView tv_title;
    private TextView tv_content;
    private TextView tv_type;

    private ListView lv;
    private View items;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获得需要的控件
        lv = findViewById(R.id.lv);
        initListDate();


    }

    //先拿到需要展示的数据列表
    public void initListDate() {

        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    //从服务器上获取xml文件
                    String path = "http://192.168.0.102:8080/news.xml";
                    URL url = new URL(path);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    conn.setConnectTimeout(5000);
                    int code = conn.getResponseCode();
                    if (code == 200) {
                        InputStream in = conn.getInputStream();

                        //写一个自己的解析xml文件的方法
                        newsList = XmlParseUtils.ParseXml(in);
                        System.out.println("newsList==" + newsList.size());
                        //设置自己的adapter
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                lv.setAdapter(new MyAdapter());

                            }
                        });

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();


    }


    public class MyAdapter extends BaseAdapter {


        @Override
        public int getCount() {
            return newsList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {


            if (convertView == null) {
//                LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
//               items = inflater.inflate(R.layout.layout,null);

                items = View.inflate(getApplicationContext(), R.layout.layout2, null);


            } else {

                items = convertView;
            }
            SmartImageView iv = items.findViewById(R.id.iv_icon);
            tv_title = items.findViewById(R.id.tv_title);
            tv_content = items.findViewById(R.id.tv_content);
            tv_type = items.findViewById(R.id.tv_type);
            iv.setImageUrl(newsList.get(position).getImage());
            System.out.println(newsList.get(position).getImage());

//                MainActivity mainActivity = new MainActivity();
//                mainActivity.getImage(newsList.get(position));
            tv_title.setText(newsList.get(position).getTitle());
            tv_content.setText(newsList.get(position).getDescription());
            if (newsList.get(position).getType().equals("1")) {
                tv_type.setText(newsList.get(position).getComment()+"跟帖");
            } else if (newsList.get(position).getType().equals("2")) {
                tv_type.setText("专题");
            } else if (newsList.get(position).getType().equals("3")) {
                tv_type.setText("跟帖2");
            }


            return items;
        }
    }

}
News
package com.zhongkechuangda.myapplication;

public class News {

    private String title;
    private String description;
    private String image;
    private String type;
    private String comment;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    @Override
    public String toString() {
        return "News{" +
                "title='" + title + '\'' +
                ", description='" + description + '\'' +
                ", image='" + image + '\'' +
                ", type='" + type + '\'' +
                ", comment='" + comment + '\'' +
                '}';
    }
}
XmlParseUtils
package com.zhongkechuangda.myapplication;

import android.util.Xml;

import org.xmlpull.v1.XmlPullParser;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class XmlParseUtils {


    private static List newsList;
    private static News news;

    public static List ParseXml(InputStream in) {

        try {
            //获得一个xml解析器
            XmlPullParser parser = Xml.newPullParser();
            parser.setInput(in, "utf-8");
            //获得要解析事件类型
            int type = parser.getEventType();
            while (type != XmlPullParser.END_DOCUMENT) {
                switch (type) {
                    //解析标签一定要从头开始啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊
                    case XmlPullParser.START_TAG:
                        if("newss".equals(parser.getName())){
                        newsList = new ArrayList<>();
                    }
                        if ("news".equals(parser.getName())) {
                            news = new News();
                        } else if ("title".equals(parser.getName())) {

                            news.setTitle(parser.nextText());
                        } else if ("description".equals(parser.getName())) {
                            news.setDescription(parser.nextText());
                        } else if ("image".equals(parser.getName())) {
                            news.setImage(parser.nextText());
                        } else if ("type".equals(parser.getName())) {
                            news.setType(parser.nextText());
                        } else if ("comment".equals(parser.getName())) {
                            news.setComment(parser.nextText());
                        }

                        break;
                    case XmlPullParser.END_TAG:
                        if ("news".equals(parser.getName())) {

                            newsList.add(news);
                        }
                        break;
                }


                //一直向下解析
                type = parser.next();
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return newsList;

    }
}

只想说解析xml一定要记住呀,所有的tag都要解析(因为我没有解析第一个newss标签 导致客户端只显示一个新闻信息 返回的newsList.size一直是1,其实还是没太想明白)

其中用了一个开源项目 smartImageView(初次体验好用的开源项目 内心窃喜,感受来自安卓大牛的呵护 手动滑稽)

效果图:

Android 学习之路 新闻客户端小案例(齐全哦)_第1张图片

没有美工小姐姐给图片,只好自己找了几张。。。

你可能感兴趣的:(Android 学习之路 新闻客户端小案例(齐全哦))