新闻客户端案例

1.知识点:XML文件的在线解析,适配器,ListView控件的使用。

2.文件目录:



3.使用的图片:


4.NewsInfo.xml文件:
<?xml version =“1.0”encoding =“UTF-8”?>
  <新闻>
     <报纸资讯>
      <图标> http://10.51.9.168:8080/images1/a.jpg </图标>
      <TITLE>科技温暖世界</ TITLE>
      <内容>进入一个更有爱的领域</内容>
      <类型> 1 </类型>
      <评价> 69 </评论>
     </报纸资讯>
	 
     <报纸资讯>
      <图标> http://10.51.9.168:8080/images1/b.jpg </图标>
      <TITLE>“神武”</ TITLE>
      <内容>新美术资源盘点,视觉新体验</内容>
      <类型> 2 </类型>
      <评价> 35 </评论>
     </报纸资讯>

     <报纸资讯>
      <图标> http://10.51.9.168:8080/images1/c.jpg </图标>
      <TITLE>南北车正式公布合并</ TITLE>
      <内容>南北车将于今日正式公布合并</内容>
      <类型> 3 </类型>
      <评价> 2 </评论>
     </报纸资讯>

     <报纸资讯>
      <图标> http://10.51.9.168:8080/images1/d.jpg </图标>
      <TITLE>萌呆了!汪星人抱玩偶酣睡</ TITLE>
      <内容>汪星人抱玩偶酣睡,萌翻网友</内容>
      <类型> 1 </类型>
      <评价> 25 </评论>
     </报纸资讯>
	 
     <报纸资讯>
      <图标> http://10.51.9.168:8080/images1/e.jpg </图标>
      <TITLE>风力发电进校园</ TITLE>
      <内容>风力发电普进校园</内容>
      <类型> 2 </类型>
      <评价> 26 </评论>
     </报纸资讯>
     
	 
     <报纸资讯>
      <图标> http://10.51.9.168:8080/images1/f.jpg </图标>
      <TITLE>地球一小时</ TITLE>
      <内容>地球熄灯一小时</内容>
      <类型> 1 </类型>
      <评价> 23 </评论>
     </报纸资讯>
        
     <报纸资讯>
      <图标> http://10.51.9.168:8080/images1/g.jpg </图标>
      <TITLE>最美公路</ TITLE>
      <内容>最美公路,难以想象</内容>
      <类型> 1 </类型>
      <评价> 23 </评论>
     </报纸资讯>
  </新闻>
5.布局文件:

  activity.xml文件:     

<?xml version =“1.0”encoding =“utf-8”?>
<RelativeLayout xmlns:android =“http://schemas.android.com/apk/res/android”
    的xmlns:工具= “http://schemas.android.com/tools”
    机器人:ID = “@ + ID / activity_main”
    机器人:layout_width = “match_parent”
    机器人:layout_height = “match_parent”
    工具:上下文= “cn.edu.bzu.news.MainActivity”>
    <的LinearLayout
        机器人:ID = “@ + ID /线路1”
        机器人:layout_width = “match_parent”
        机器人:layout_height = “WRAP_CONTENT”>
        <ListView的
            机器人:ID = “@ + ID /列表1”
            机器人:layout_width = “match_parent”
            机器人:layout_height = “WRAP_CONTENT”>
        </的ListView>
    </的LinearLayout>

</ RelativeLayout的>
item.xml文件:

<?xml version =“1.0”encoding =“utf-8”?>
<RelativeLayout xmlns:android =“http://schemas.android.com/apk/res/android”
    机器人:layout_width = “match_parent”
    机器人:layout_height = “match_parent”>
    <com.loopj.android.image.SmartImageView
        机器人:ID = “@ + ID / item_image”
        机器人:layout_width = “80dp”
        机器人:layout_height = “60dp”
        android:scaleType =“centerCrop”/>
    <TextView的
        机器人:ID = “@ + ID / item_tv1”
        机器人:layout_width = “WRAP_CONTENT”
        机器人:layout_height = “WRAP_CONTENT”
        android:layout_toRightOf =“@ id / item_image”/>
    <TextView的
        机器人:ID = “@ + ID / item_tv2”
        机器人:layout_width = “WRAP_CONTENT”
        机器人:layout_height = “WRAP_CONTENT”
        机器人:layout_below = “@ ID / item_tv1”
        机器人:layout_toRightOf = “@ ID / item_image”/>
    <TextView的
        机器人:ID = “@ + ID / item_tv3”
        机器人:layout_width = “WRAP_CONTENT”
        机器人:layout_height = “WRAP_CONTENT”
        机器人:layout_alignParentRight = “真”/>
</ RelativeLayout的>

6.MainActivity.java:
包cn.edu.bzu.news;

导入android.support.v7.app.AppCompatActivity;
导入android.os.Bundle;
导入android.widget.ListView;
导入android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

import java.io.ByteArrayInputStream;
import java.util.List;

import cn.edu.bzu.news.adapter.MyAdapter;
进口cn.edu.bzu.news.entity.NewsInfo;
import cn.edu.bzu.news.service.NewsInfoService;

公共课MainActivity扩展AppCompatActivity {


    私有ListView listView;
    私人MyAdapter适配器
    private List <NewsInfo> newsInfos;

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        的ListView =(ListView中)findViewById(R.id.list1);
        加载();
    }

    public void load()
    {
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        asyncHttpClient.get(getString(R.string.servelurl)),新的AsyncHttpResponseHandler(){
                @覆盖
            public void onSuccess(String content){
                super.onSuccess(内容);
                byte [] bytes = content.getBytes();
                ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
                newsInfos = NewsInfoService.getNewsInfo(bais);
                if(newsInfos == null)Toast.makeText(MainActivity.this,“解析失败”,Toast.LENGTH_LONG).show();
                else {
                    adapter = new MyAdapter(MainActivity.this,R.layout.item,newsInfos);
                    listView.setAdapter(适配器);
                    Toast.makeText(MainActivity.this, “成功”,Toast.LENGTH_LONG).show();
                }
            }

            @覆盖
            public void onFailure(Throwable error,String content){
                super.onFailure(误差,内容);
                Toast.makeText(MainActivity.this, “请求失败”,Toast.LENGTH_LONG).show();
            }
        });
    }

}
7.NewsInfo.java:

包cn.edu.bzu.news.entity;


public class NewsInfo {
    私人字符串路径;
    私人字符串标题;
    私人字符串
    私人长评论

    public void setDes(String des){
        this.des = des
    }

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

    public void setPath(String path){
        this.path = path;
    }

    public void setComment(long comment){
        this.comment =评论
    }

    public String getPath(){
        回路
    }

    public String getTitle(){
        返回标题
    }

    public String getDes(){
        回报
    }

    public long getComment(){
        返回评论
    }


}

8.NewsInfoService.java:

包cn.edu.bzu.news.service;

导入android.util.Xml;
import org.xmlpull.v1.XmlPullParser;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
进口cn.edu.bzu.news.entity.NewsInfo;



public class NewsInfoService {

    public static List <NewsInfo> getNewsInfo(InputStream inputStream)
    {
        XmlPullParser parser = Xml.newPullParser();
        尝试{
            parser.setInput(的inputStream, “UTF-8”);
            int type = parser.getEventType();
            列表<NewsInfo> newsInfos = null;
            NewsInfo newsInfo = null;
            而(键入!= XmlPullParser.END_DOCUMENT)
            {
                开关(类型){
                    case XmlPullParser.START_TAG:
                        if(“news”.equals(parser.getName()))
                            newsInfos = new ArrayList <NewsInfo>();
                        else if(“newsInfo”.equals(parser.getName()))
                            newsInfo = new NewsInfo();
                        else if(“icon”.equals(parser.getName())){
                            String icon = parser.nextText();
                            newsInfo.setPath(图标);
                        } else if(“title”.equals(parser.getName())){
                            String title = parser.nextText();
                            newsInfo.setTitle(标题);
                        } else if(“content”.equals(parser.getName())){
                            String des = parser.nextText();
                            newsInfo.setDes(DES);
                        } else if(“comment”.equals(parser.getName())){
                            String comment = parser.nextText();
                            newsInfo.setComment(的Long.parseLong(评论));
                        }
                        打破;
                    case XmlPullParser.END_TAG:
                        if(“newsInfo”.equals(parser.getName())){
                            newsInfos.add(报纸资讯);
                            newsInfo = null;
                        }
                        打破;
                    }
                类型= parser.next();
            }
            回报新闻
        } catch(Exception e){
            e.printStackTrace();
            返回null;
        }

    }
}

9.MyAdapter.java:
包cn.edu.bzu.news.adapter;

导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.View;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.TextView;
import com.loopj.android.image.SmartImageView;
import java.util.List;
进口cn.edu.bzu.news.R;
进口cn.edu.bzu.news.entity.NewsInfo;



public class MyAdapter extends ArrayAdapter <NewsInfo> {

    private int resourceId;

    public MyAdapter(Context context,int textViewResourceId,List <NewsInfo> object){
        super(context,textViewResourceId,object);
        resourceId = textViewResourceId;
    }

    public View getView(int position,View convertView,ViewGroup parent)
    {
        NewsInfo info = getItem(position);
        layoutInflater inflater =(LayoutInflater)getContext()。getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        查看view = inflater.inflate(resourceId,null);

        SmartImageView imageView =(SmartImageView)view.findViewById(R.id.item_image);
        TextView tv1 =(TextView)view.findViewById(R.id.item_tv1);
        TextView tv2 =(TextView)view.findViewById(R.id.item_tv2);
        TextView tv3 =(TextView)view.findViewById(R.id.item_tv3);

        imageView.setImageUrl(info.getPath());
        tv1.setText(info.getTitle());
        tv2.setText(info.getDes());
        tv3.setText(将String.valueOf(info.getComment()));

        返回视图;
    }

}
9.运行结果图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值