修改main.java,調用前面的類,從intentert擷取rss列表並顯示在ui上:
public final String RSS_URL = "http://blog.sina.com.cn/rss/1267454277.xml";//從網路擷取RSS地址
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);//調用getFeed方法,從伺服器取得rss提要
showListView(); //把rss內容綁定到ui介面進行顯示
}
private RSSFeed getFeed(String urlString) //該方法通過url獲得xml並解析xml內容為RSSFeed對象
{
try
{
URL url = new URL(urlString);
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader xmlreader = parser.getXMLReader();
RSSHandler 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("訪問的RSS無效");
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); //listview綁定適配器
itemlist.setOnItemClickListener(this); //設定itemclick事件代理
itemlist.setSelection(0);
}
public void onItemClick(AdapterView parent, View v, int position, long id) //itemclick事件代理方法
{
Intent itemintent = new Intent(this,ActivityShowDescription.class);//構建一個“意圖”,用於指向activity :detail
Bundle b = new Bundle(); //構建buddle,並將要傳遞參數都放入buddle
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); //用android.intent.extra.INTENT的名字來傳遞參數
startActivityForResult(itemintent, 0);
}
}
到此,程式已經可以顯示第1個activity(頁面)了。但由於程式使用了網路,我們還必須在AndroidManifest.xml中增加使用網路的許可權:
當我們點擊一個item的時候,頁面要跳轉到另一個Activity,用來展現item裡面的內容,所以需要建立一個新的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 = "不好意思程式出錯啦";
} else {
content = bundle.getString("title") + "\n\n"
+ bundle.getString("pubdate") + "\n\n"
+ bundle.getString("description").replace('\n', ' ')
+ "\n\n詳細資料請訪問以下網址:\n" + bundle.getString("link");
}
} else {
content = "不好意思程式出錯啦";
}
TextView 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();
}
});
}
}
它對應的xml檔案如下:
<?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="返回"
android:id="@+id/back"
/>
</LinearLayout>
此外還需要在AndroidMainfest.xml裡添加<activity android:name=".ActivityShowDescription"></activity>
這樣我們就完成了這個RSS小程式,運行效果如下: