A quick article on how to remove the first two entries from a RSS feed produced by a bunch of java files collectively known as "SimpleRssReader.
Why?
The original file reads of a feed from PCWorld.com. I adapted this to read a google news rss feed and the first two entries are "<query> - Google News", everytime. This article documents what I did to fix it.
How?
Basic to some but I'm new to the game. I'm trying to remove the first two entries from a news feed from a Google News feed.
Modifying the strings can be done in the PcWorldRssParser.java (without it crashing the app). The gist is to check for a description (the first two items do not have one). The following will also return a publishing date:
Find the List function in:
PcWorldRssParser.java (readFeed) and replace with
private List<RssItem> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException { parser.require(XmlPullParser.START_TAG, null, "rss"); String title = null; String link = null; String date = null; String desc = null; List<RssItem> items = new ArrayList<RssItem>(); while (parser.next() != XmlPullParser.END_DOCUMENT) { if (parser.getEventType() != XmlPullParser.START_TAG) { continue; } String name = parser.getName(); if (name.equals("title")) { title = readTitle(parser); } else if (name.equals("link")) { link = readLink(parser); } else if (name.equals("pubDate")) { date = readDate(parser); } else if (name.equals("description")) { desc = readDesc(parser); } if (title != null && link != null && desc != null) { RssItem item = new RssItem(title, link, date, desc); items.add(item); title = null; link = null; date = null; desc = null; } } return items; } private String readDate(XmlPullParser parser) throws XmlPullParserException, IOException { parser.require(XmlPullParser.START_TAG, ns, "pubDate"); String date = readText(parser); parser.require(XmlPullParser.END_TAG, ns, "pubDate"); return date; } private String readDesc(XmlPullParser parser) throws XmlPullParserException, IOException { parser.require(XmlPullParser.START_TAG, ns, "description"); String desc = readText(parser); parser.require(XmlPullParser.END_TAG, ns, "description"); return desc; }
- private List<RssItem> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
- parser.require(XmlPullParser.START_TAG, null, "rss");
- String title = null;
- String link = null;
- String date = null;
- String desc = null;
- List<RssItem> items = new ArrayList<RssItem>();
- while (parser.next() != XmlPullParser.END_DOCUMENT) {
- if (parser.getEventType() != XmlPullParser.START_TAG) {
- continue;
- }
- String name = parser.getName();
- if (name.equals("title")) {
- title = readTitle(parser);
- } else if (name.equals("link")) {
- link = readLink(parser);
- } else if (name.equals("pubDate")) {
- date = readDate(parser);
- } else if (name.equals("description")) {
- desc = readDesc(parser);
- }
- if (title != null && link != null && desc != null) {
- RssItem item = new RssItem(title, link, date, desc);
- items.add(item);
- title = null;
- link = null;
- date = null;
- desc = null;
- }
- }
- return items;
- }
- private String readDate(XmlPullParser parser) throws XmlPullParserException, IOException {
- parser.require(XmlPullParser.START_TAG, ns, "pubDate");
- String date = readText(parser);
- parser.require(XmlPullParser.END_TAG, ns, "pubDate");
- return date;
- }
- private String readDesc(XmlPullParser parser) throws XmlPullParserException, IOException {
- parser.require(XmlPullParser.START_TAG, ns, "description");
- String desc = readText(parser);
- parser.require(XmlPullParser.END_TAG, ns, "description");
- return desc;
- }
Modify for date and description
RssAdapter.java (getView)
@Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = View.inflate(context, R.layout.rss_item, null); holder = new ViewHolder(); holder.newsItemTitle = (TextView) convertView.findViewById(R.id.newsItemTitle); holder.newsItemDateD = (TextView) convertView.findViewById(R.id.newsItemDateD); holder.newsItemDateM = (TextView) convertView.findViewById(R.id.newsItemDateM); holder.newsItemDesc = (TextView) convertView.findViewById(R.id.newsItemDesc); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.newsItemTitle.setText(items.get(position).getTitle()); holder.newsItemDateD.setText(items.get(position).getDate().substring(5, 7)); holder.newsItemDateM.setText(items.get(position).getDate().substring(8, 11).toUpperCase()); holder.newsItemDesc.setText(items.get(position).getDesc()); return convertView; } static class ViewHolder { TextView newsItemTitle; TextView newsItemDateD; TextView newsItemDateM; TextView newsItemDesc; }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder holder;
- if (convertView == null) {
- convertView = View.inflate(context, R.layout.rss_item, null);
- holder = new ViewHolder();
- holder.newsItemTitle = (TextView) convertView.findViewById(R.id.newsItemTitle);
- holder.newsItemDateD = (TextView) convertView.findViewById(R.id.newsItemDateD);
- holder.newsItemDateM = (TextView) convertView.findViewById(R.id.newsItemDateM);
- holder.newsItemDesc = (TextView) convertView.findViewById(R.id.newsItemDesc);
- convertView.setTag(holder);
- } else {
- holder = (ViewHolder) convertView.getTag();
- }
- holder.newsItemTitle.setText(items.get(position).getTitle());
- holder.newsItemDateD.setText(items.get(position).getDate().substring(5, 7));
- holder.newsItemDateM.setText(items.get(position).getDate().substring(8, 11).toUpperCase());
- holder.newsItemDesc.setText(items.get(position).getDesc());
- return convertView;
- }
- static class ViewHolder {
- TextView newsItemTitle;
- TextView newsItemDateD;
- TextView newsItemDateM;
- TextView newsItemDesc;
- }
RssItem.java
public class RssItem { private String title; private String link; private String date; private String desc; public RssItem(String title, String link, String date, String desc) { this.title = title; this.link = link; this.date = date; this.desc = desc; } public String getTitle() { return title; } public String getLink() { return link; } public String getDate() { return date; } public String getDesc() { return desc; } }
- public class RssItem {
- private String title;
- private String link;
- private String date;
- private String desc;
- public RssItem(String title, String link, String date, String desc) {
- this.title = title;
- this.link = link;
- this.date = date;
- this.desc = desc;
- }
- public String getTitle() {
- return title;
- }
- public String getLink() {
- return link;
- }
- public String getDate() {
- return date;
- }
- public String getDesc() {
- return desc;
- }
- }
My rss_item.xml (reduced down):
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/newsItemDateD" android:maxLines="1" android:gravity="center|bottom" android:layout_height="50dip" android:layout_width="70dip" android:textSize="30sp" /> <TextView android:id="@+id/newsItemDateM" android:maxLines="1" android:gravity="center|top" android:layout_height="30dip" android:layout_width="70dip" android:layout_below="@+id/newsItemDateD" /> <TextView android:id="@+id/newsItemTitle" android:layout_toRightOf="@+id/newsItemDateD" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/newsItemDesc" android:layout_alignLeft="@+id/newsItemTitle" android:layout_below="@+id/newsItemTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical">
- <TextView
- android:id="@+id/newsItemDateD"
- android:maxLines="1"
- android:gravity="center|bottom"
- android:layout_height="50dip"
- android:layout_width="70dip"
- android:textSize="30sp" />
- <TextView
- android:id="@+id/newsItemDateM"
- android:maxLines="1"
- android:gravity="center|top"
- android:layout_height="30dip"
- android:layout_width="70dip"
- android:layout_below="@+id/newsItemDateD" />
- <TextView
- android:id="@+id/newsItemTitle"
- android:layout_toRightOf="@+id/newsItemDateD"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <TextView
- android:id="@+id/newsItemDesc"
- android:layout_alignLeft="@+id/newsItemTitle"
- android:layout_below="@+id/newsItemTitle"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- </RelativeLayout>
Sources:
- Android Research Blog - Creating A Simple RSS Application in Android
- Android Research Blog - Creating a Simple Rss Application in Android (V2)
- Github - Vgrec - SimpleRssReader.java