Modify main. Java and call the previous class to obtain the RSS list from intentert and display it on the UI:
Public final string rss_url = "http://blog.sina.com.cn/rss/1267454277.xml"; // get the RSS address from the network
Public final string tag = "rssreader ";
Private rssfeed feed = NULL;
/** Called when the activity is first created .*/
Public void oncreate (bundle icicle ){
Super. oncreate (icicle );
Setcontentview (R. layout. Main );
Feed = getfeed (rss_url); // call the getfeed method to obtainRSSAbstract
Showlistview (); // setRSSBind contentUIInterface display
}
Private rssfeed getfeed (string urlstring) // This method usesURLObtainXMLAnd parseXMLThe content is an rssfeed object.
{
Try
{
URL url = new URL (urlstring );
Saxparserfactory factory = saxparserfactory. newinstance ();
Saxparser parser = factory. newsaxparser ();
Xmlreader = parser. getxmlreader ();
Rsshandler = new rsshandler ();
Xmlreader. setcontenthandler (rsshandler );
Inputsource is = new inputsource (URL. openstream ());
Xmlreader. parse (is );
Return rsshandler. getfeed ();
}
Catch (exception ee)
{
Return NULL;
}
}
Private void showlistview ()
{
Listview itemlist = (listview) findviewbyid (R. Id. itemlist );
If (feed = NULL)
{
Settitle ("the accessed RSS is invalid ");
Return;
}
Simpleadapter adapter = new simpleadapter (this, feed. getallitemsforlistview (),
Android. R. layout. simple_list_item_2, new string [] {rssitem. Title, rssitem. pubdate },
New int [] {Android. R. Id. text1, Android. R. Id. text2 });
Itemlist. setadapter (adapter); // bind an adapter to the listview
Itemlist. setonitemclicklistener (this); // setItemclickEvent proxy
Itemlist. setselection (0 );
}
Public void onitemclick (adapterview parent, view V, int position, long ID) // itemclick event proxy method
{
Intent itemintent = new intent (this, activityshowdescription. Class); // construct an "intent" to point to activity: Detail
Bundle B = new bundle (); // buildBuddleAnd put all the parameters to be passedBuddle
B. putstring ("title", feed. getitem (position). gettitle ());
B. putstring ("Description", feed. getitem (position). getdescription ());
B. putstring ("Link", feed. getitem (position). getlink ());
B. putstring ("pubdate", feed. getitem (position). getpubdate ());
Itemintent. putextra ("android. Intent. Extra. rssitem", B); // use the name of Android. Intent. Extra. Intent to pass the Parameter
Startactivityforresult (itemintent, 0 );
}
}
At this point, the program can display 1st activities (pages. However, because the program uses the network, we must add the permission to use the network in androidmanifest. xml:
When we click an item, the page will jump to another activity to display the content in the item, so we need to create a new activity: activityshowdescription.
Public class activityshowdescription extends activity {
Public void oncreate (bundle icicle ){
Super. oncreate (icicle );
Setcontentview (R. layout. showdescription );
String content = NULL;
Intent startingintent = getintent ();
If (startingintent! = NULL ){
Bundle bundle = startingintent
. Getbundleextra ("android. Intent. Extra. rssitem ");
If (bundle = NULL ){
Content = "sorry, program error ";
} Else {
Content = bundle. getstring ("title") + "\ n"
+ Bundle. getstring ("pubdate") + "\ n"
+ Bundle. getstring ("Description"). Replace ('\ n ','')
+ "\ N for details, visit the following url: \ n" + bundle. getstring ("Link ");
}
} Else {
Content = "sorry, program error ";
}
Textview = (textview) findviewbyid (R. Id. content );
Textview. settext (content );
Button backbutton = (button) findviewbyid (R. Id. Back );
Backbutton. setonclicklistener (New button. onclicklistener (){
Public void onclick (view v ){
Finish ();
}
});
}
}
The corresponding XML file is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: autolink = "all"
Android: text = ""
Android: Id = "@ + ID/content"
Android: layout_weight = "1.0"
/>
<Button
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "return"
Android: Id = "@ + ID/back"
/>
</Linearlayout>
In addition, you must add <activity Android: Name = ". activityshowdescription"> </activity> to androidmainfest. xml.
In this way, we have completed this RSS Applet and the running effect is as follows: