在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Android平台开发 → 一个基于RSS的新闻应用

一个基于RSS的新闻应用

Android平台开发

下载此实例
  • 开发语言:Java
  • 实例大小:8.88M
  • 下载次数:14
  • 浏览次数:479
  • 发布时间:2016-08-17
  • 实例类别:Android平台开发
  • 发 布 人:Mygica可爱猪猪
  • 文件格式:.zip
  • 所需积分:2
 相关标签: Rss Android 新闻

实例介绍

【实例简介】一个基于RSS的新闻应用

【实例截图】

【核心代码】

package com.example.lhhxkj.news_application;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.lang.String;

public class MainActivity extends AppCompatActivity {
    ArrayAdapter<String> adapterMainSubjects;
    ListView myMainListView;

    // NEWS categories 
    String[][] myUrlCaptionMenu = {
            {"http://www.npr.org/rss/rss.php?id=1001", "Arts & Entertainment"},
            {"http://www.npr.org/rss/rss.php?id=1003", "Business"},
            {"http://www.npr.org/rss/rss.php?id=1004", "World News"},
            {"http://www.npr.org/rss/rss.php?id=1006", "U.S. News"},
            {"http://www.npr.org/rss/rss.php?id=1008", "People & Places"},
            {"http://www.npr.org/rss/rss.php?id=1012", "Politics & Society"},
            {"http://www.npr.org/rss/rss.php?id=1021", "Top Stories"},
            {"http://www.npr.org/rss/rss.php?id=1057", "Opinion"}
    };
    //define convenient URL and CAPTIONs arrays
    String[] myUrlCaption = new String[myUrlCaptionMenu.length];
    String[] myUrlAddress = new String[myUrlCaptionMenu.length];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setLogo(R.mipmap.ic_launcher);//为Toolbar设置Logo
        setSupportActionBar(toolbar);
        for (int i = 0; i < myUrlCaptionMenu.length; i ) {
            myUrlAddress[i] = myUrlCaptionMenu[i][0];
            myUrlCaption[i] = myUrlCaptionMenu[i][1];
        }

        // clicking on a row shows dialogBox with more info about selected item
        myMainListView = (ListView) this.findViewById(R.id.myListView);
        myMainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> _av, View _v,
                                    int _index, long _id) {
                String urlAddress = myUrlAddress[_index];
                String urlCaption = myUrlCaption[_index];
                int count = 0;
                count ;
                //create an Intent to talk to activity: ShowHeadlines
                Intent callShowHeadLines = new Intent(MainActivity.this,
                        ShowHeadLines.class);
                //prepare a Bundle and add the input arguments: url & caption
                Bundle myData = new Bundle();
                myData.putString("urlAddress", urlAddress);
                myData.putString("urlCaption", urlCaption);
                callShowHeadLines.putExtras(myData);
                startActivity(callShowHeadLines);
            }//end onItemClick
        });//end setOnItemClickListener

        // fill up the Main‐GUI’s ListView with main news categories
        adapterMainSubjects = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myUrlCaption);//simple_list_item_1:android default
        myMainListView.setAdapter(adapterMainSubjects);
    }//onCreate

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_history) {
            return true;
        }
        else if (id == R.id.action_goBack) {
            Intent intent = new Intent(MainActivity.this, MainActivity.class);
            startActivity(intent);
        }
        else if (id == R.id.action_about) {
            AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
            dialog.setTitle("About the News_application");
            dialog.setIcon(R.mipmap.ic_launcher);
            dialog.setMessage("This is a simple News Application. You can read news and learn more details\n"
                    "about the news you are interested in.");
            dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            });
            dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            });
            dialog.show();
        }//end if

       else if (id == R.id.action_exit) {
            android.os.Process.killProcess(android.os.Process.myPid());
        }
        return super.onOptionsItemSelected(item);
    }
}


(6)ShowHeadLines.java

package com.example.lhhxkj.news_application;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.lang.String;
import java.util.ArrayList;
/**
 * Created by lhhxkj on 2016/5/22.
 */
public class ShowHeadLines extends AppCompatActivity {
    
    ArrayList<SingleItem> newsList =new ArrayList<SingleItem>();
    ListView myListView;
    String urlAddress ="";
    String urlCaption ="";
    SingleItem selectedNewsItem;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.storylist);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar2);
        toolbar.setTitle("Story Title");   //set the title
        //toolbar.setLogo(R.drawable.ic_stat_name);  //add the icon
        setSupportActionBar(toolbar);

        //set up an actionbar
        //ActionBar actionBar = getActionBar();
        myListView = (ListView) this.findViewById(R.id.myListView);
        // find out which intent is calling us
        Intent callingIntent = getIntent();
        // grab data bundle holding selected url & caption sent to us
        Bundle myBundle = callingIntent.getExtras();
        urlAddress = myBundle.getString("urlAddress");
        urlCaption = myBundle.getString("urlCaption");
        // update app's top 'TitleBar' (eg. 'NPR ‐ Business Wed April 09, 2014')       
        //this.setTitle("NPR ‐ " urlCaption " \t" );

        // clicking on a row shows dialogBox with more info about selected item
        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> av, View v, int index, long id) {
                selectedNewsItem = newsList.get(index);
                showNiceDialogBox(selectedNewsItem, getApplicationContext());
            }
        });
        // get stories for the selected news option
        DownloadRssFeed downloader = new DownloadRssFeed(ShowHeadLines.this);
        downloader.execute(urlAddress, urlCaption);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(ShowHeadLines.this,MainActivity.class);
                startActivity(intent);
            }
        });
    }//onCreate

    public void showNiceDialogBox(SingleItem selectedStoryItem,
                                  Context context){
        // make a nice looking dialog box (story summary, btnClose, btnMore)
        // CAUTION: (check)on occasions title and description are the same!
        String title = selectedStoryItem.getTitle();
        String description = selectedStoryItem.getDescription();
        if (title.toLowerCase().equals(description.toLowerCase())){
            description = "";
        }
        try {
                //CAUTION: sometimes TITLE and DESCRIPTION include HTML markers
                final Uri storyLink = Uri.parse(selectedStoryItem.getLink());
                AlertDialog.Builder myBuilder = new AlertDialog.Builder(this);
                myBuilder
                        .setTitle(Html.fromHtml(urlCaption))
                        .setMessage(title "\n\n" Html.fromHtml(description) "\n")
                .setPositiveButton("Close", null)
                .setNegativeButton("More",new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface dialog,int whichOne){
                        Intent browser = new Intent(Intent.ACTION_VIEW,storyLink);
                        startActivity(browser);
                    }
                })//setNegativeButton
                .show();
            }catch (Exception e) {
                Log.e("Error DialogBox", e.getMessage());
            }
        }//showNiceDialogBox


    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.storylist_menu, menu);
        return true;
    }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_history) {
                return true;
            }
            if (id == R.id.action_about) {
                AlertDialog.Builder dialog= new AlertDialog.Builder(ShowHeadLines.this);
                dialog.setTitle("About the News_application");
                dialog.setIcon(R.mipmap.ic_launcher);
                dialog.setMessage("This is a simple News Application. You can read news and learn more details\n"
                        "about the news you are interested in.");
                dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
                dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
                dialog.show();
            }
            return super.onOptionsItemSelected(item);
        }
}//ShowHeadLines

标签: Rss Android 新闻

实例下载地址

一个基于RSS的新闻应用

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警