修改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小程序,运行效果如下: