Android Java: Remove first two entries from Google News feed in SimpleRssReader

What?
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
copyraw
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;
    }
  1.  private List<RssItem> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException { 
  2.          parser.require(XmlPullParser.START_TAG, null, "rss")
  3.          String title = null
  4.          String link = null
  5.          String date = null
  6.          String desc = null
  7.          List<RssItem> items = new ArrayList<RssItem>()
  8.          while (parser.next() != XmlPullParser.END_DOCUMENT) { 
  9.              if (parser.getEventType() != XmlPullParser.START_TAG) { 
  10.                  continue; 
  11.              } 
  12.              String name = parser.getName()
  13.              if (name.equals("title")) { 
  14.                  title = readTitle(parser)
  15.              } else if (name.equals("link")) { 
  16.                  link = readLink(parser)
  17.              } else if (name.equals("pubDate")) { 
  18.                  date = readDate(parser)
  19.              } else if (name.equals("description")) { 
  20.                  desc = readDesc(parser)
  21.              } 
  22.                  if (title != null && link != null && desc != null) { 
  23.   
  24.                      RssItem item = new RssItem(title, link, date, desc)
  25.                      items.add(item)
  26.                      title = null
  27.                      link = null
  28.                      date = null
  29.                      desc = null
  30.                  } 
  31.   
  32.          } 
  33.          return items; 
  34.      } 
  35.   
  36.      private String readDate(XmlPullParser parser) throws XmlPullParserException, IOException { 
  37.          parser.require(XmlPullParser.START_TAG, ns, "pubDate")
  38.          String date = readText(parser)
  39.          parser.require(XmlPullParser.END_TAG, ns, "pubDate")
  40.          return date; 
  41.      } 
  42.      private String readDesc(XmlPullParser parser) throws XmlPullParserException, IOException { 
  43.          parser.require(XmlPullParser.START_TAG, ns, "description")
  44.          String desc = readText(parser)
  45.          parser.require(XmlPullParser.END_TAG, ns, "description")
  46.          return desc; 
  47.      } 


Modify for date and description
RssAdapter.java (getView)
copyraw
@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;
    }
  1.  @Override 
  2.      public View getView(int position, View convertView, ViewGroup parent) { 
  3.          ViewHolder holder; 
  4.          if (convertView == null) { 
  5.              convertView = View.inflate(context, R.layout.rss_item, null)
  6.              holder = new ViewHolder()
  7.              holder.newsItemTitle = (TextView) convertView.findViewById(R.id.newsItemTitle)
  8.              holder.newsItemDateD = (TextView) convertView.findViewById(R.id.newsItemDateD)
  9.              holder.newsItemDateM = (TextView) convertView.findViewById(R.id.newsItemDateM)
  10.              holder.newsItemDesc = (TextView) convertView.findViewById(R.id.newsItemDesc)
  11.              convertView.setTag(holder)
  12.          } else { 
  13.              holder = (ViewHolder) convertView.getTag()
  14.          } 
  15.          holder.newsItemTitle.setText(items.get(position).getTitle())
  16.          holder.newsItemDateD.setText(items.get(position).getDate().substring(5, 7))
  17.          holder.newsItemDateM.setText(items.get(position).getDate().substring(8, 11).toUpperCase())
  18.          holder.newsItemDesc.setText(items.get(position).getDesc())
  19.          return convertView; 
  20.      } 
  21.   
  22.      static class ViewHolder { 
  23.          TextView newsItemTitle; 
  24.          TextView newsItemDateD; 
  25.          TextView newsItemDateM; 
  26.          TextView newsItemDesc; 
  27.      } 

RssItem.java
copyraw
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;
    }

}
  1.  public class RssItem { 
  2.   
  3.      private String title; 
  4.      private String link; 
  5.      private String date; 
  6.      private String desc; 
  7.   
  8.      public RssItem(String title, String link, String date, String desc) { 
  9.              this.title = title; 
  10.              this.link = link; 
  11.              this.date = date; 
  12.              this.desc = desc; 
  13.      } 
  14.   
  15.      public String getTitle() { 
  16.          return title; 
  17.      } 
  18.   
  19.      public String getLink() { 
  20.          return link; 
  21.      } 
  22.   
  23.      public String getDate() { 
  24.          return date; 
  25.      } 
  26.   
  27.      public String getDesc() { 
  28.          return desc; 
  29.      } 
  30.   
  31.  } 


My rss_item.xml (reduced down):
copyraw
<?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>
  1.  <?xml version="1.0" encoding="utf-8"?> 
  2.  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.      android:layout_width="match_parent" 
  4.      android:layout_height="wrap_content" 
  5.      android:orientation="vertical"> 
  6.   
  7.      <TextView 
  8.          android:id="@+id/newsItemDateD" 
  9.          android:maxLines="1" 
  10.          android:gravity="center|bottom" 
  11.          android:layout_height="50dip" 
  12.          android:layout_width="70dip" 
  13.          android:textSize="30sp" /> 
  14.   
  15.      <TextView 
  16.          android:id="@+id/newsItemDateM" 
  17.          android:maxLines="1" 
  18.          android:gravity="center|top" 
  19.          android:layout_height="30dip" 
  20.          android:layout_width="70dip" 
  21.          android:layout_below="@+id/newsItemDateD" /> 
  22.   
  23.      <TextView 
  24.          android:id="@+id/newsItemTitle" 
  25.          android:layout_toRightOf="@+id/newsItemDateD" 
  26.          android:layout_width="wrap_content" 
  27.          android:layout_height="wrap_content" /> 
  28.   
  29.      <TextView 
  30.          android:id="@+id/newsItemDesc" 
  31.          android:layout_alignLeft="@+id/newsItemTitle" 
  32.          android:layout_below="@+id/newsItemTitle" 
  33.          android:layout_width="wrap_content" 
  34.          android:layout_height="wrap_content" /> 
  35.   
  36.  </RelativeLayout> 


Sources:
Category: AndroidOS :: Article: 590

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF
© 2024 Joel Lipman .com. All Rights Reserved.