嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元微信扫码支付:2 元
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
一个基于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