博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android实例RSS客户端开发(3)--rss列表并显示在ui上
阅读量:7079 次
发布时间:2019-06-28

本文共 3726 字,大约阅读时间需要 12 分钟。

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

转载地址:http://rqpml.baihongyu.com/

你可能感兴趣的文章
HTML导航 - 点击更改背景
查看>>
leetcode 链表 Partition List
查看>>
织梦在服务器上面安装的时候一直提示data文件没有权限,可我已经写了权限,还是提示...
查看>>
自己动手做聊天机器人 一-涉及知识【转】
查看>>
Win2008R2配置WebDeploy发布网站
查看>>
vuex
查看>>
python os 模块
查看>>
卸载完百度影音以后天气助手还在,而且总是自己主动打开ie浏览器,解决方式...
查看>>
GO语言的进阶之路-协程和Channel
查看>>
Rust 1.7.0 处理命令行參数
查看>>
Oschina 安卓client源代码学习之中的一个
查看>>
halcon连续采集图像
查看>>
GitHub分支项目不支持搜索问题解决:Sorry, forked repositories are not currently searchable....
查看>>
iphone数据存储之-- Core Data的使用(一)
查看>>
Emmet:HTML/CSS代码快速编写神器
查看>>
[原]设置vs的release版本调试
查看>>
Spark Mllib里如何将如温度、湿度和风速等数值特征字段用除以***进行标准化(图文详解)...
查看>>
Object 转换为 BigDecimal
查看>>
Apache与Nginx的优缺点比较
查看>>
python接口自动化测试(三)-requests.post()
查看>>