3

I'm building an RSS feed reader by following the example from this site:

https://www.androidpit.com/java-guide-2-program-your-own-rss-reader

At the moment, when you click on a feed title, nothing happens. What I want it to do is to open the corresponding link in a browser but I've not been able to figure it out.

I've tried this:

private String readItem(XmlPullParser parser) throws XmlPullParserException, IOException {
            String result = null;
            parser.require(XmlPullParser.START_TAG, null, "item");
            while (parser.next() != XmlPullParser.END_TAG) {
                if (parser.getEventType() != XmlPullParser.START_TAG) {
                    continue;
                }
                String name = parser.getName();
                if (name.equals("title")) {
                    result = readTitle(parser);                 
                } else {
                    skip(parser);
                }
            } 
            return result;
        }

        // Processes link tags in the feed.
        private List<String> readLink(XmlPullParser parser) throws IOException, XmlPullParserException {
            ArrayList<String> link = new ArrayList<>();
            parser.require(XmlPullParser.START_TAG, null, "link");
            link.add(readText(parser));
            parser.require(XmlPullParser.END_TAG, null, "link");
            return link;
        }

        // Processes title tags in the feed.
        private String readTitle(XmlPullParser parser) throws IOException, XmlPullParserException {
            parser.require(XmlPullParser.START_TAG, null, "title");
            String title = readText(parser);
            parser.require(XmlPullParser.END_TAG, null, "title");
            return title;
        }

I end up with a warning that says the method readLink is never used locally. I'm not sure whether I should alter the readItem method because there is a readChannel method that requires the value that is returned to be a string.

When I use public void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); ... } with a Toast, I'm able to see the position of the Item that was clicked but when I use this the app crashes.

        Uri uri = Uri.parse(link.get(position));
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);

So my question is: how do I make a browser open that goes to the corresponding title's link? I'm very new with java and android development so please bear with me.

P.S I've already set my ListView as clickable.

admiralchip
  • 127
  • 1
  • 2
  • 11
  • "the app crashes" -- use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Feb 10 '16 at 19:19
  • I test the app on my phone because the emulator is very slow so I don't think I can see the stack trace associated with the crash – admiralchip Feb 10 '16 at 19:21
  • If you can test your app on your phone by running it from your IDE, then you can access LogCat. If not, you need to fix your development environment such that you *can* access LogCat on your phone. – CommonsWare Feb 10 '16 at 19:21
  • Currently what I do is to export the app into a .apk file and copy and install it into my phone. Couldn't find the correct drivers for it to work with the IDE. – admiralchip Feb 10 '16 at 19:24
  • 1
    Then put this project aside for a while and work on either getting the emulator running acceptably, getting the correct drivers for your device, or getting a device for which you can get correct drivers. LogCat and other debugging tools are essential for Android development. – CommonsWare Feb 10 '16 at 19:26

1 Answers1

2

Right. I've figured it out so I thought I'd share it here just in case someone is looking for the same thing.

I had to add private ArrayList<String> links = new ArrayList<>(); in the PlaceholderFragment class:

public static class PlaceholderFragment extends ListFragment {
     private ArrayList<String> links = new ArrayList<>();
    ...
}

Make the following change to the readItem method:

if (name.equals("title")) {
    result = readTitle(parser);                 
} else if (name.equals("link")) {
    links.add(readLink(parser));
} else {
    skip(parser);
}

Add this:

        // Processes link tags in the feed.
        private String readLink(XmlPullParser parser) throws IOException, XmlPullParserException {
            parser.require(XmlPullParser.START_TAG, null, "link");
            String link = readText(parser);
            parser.require(XmlPullParser.END_TAG, null, "link");
            return link;
        }

Then add this:

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        Uri uri = Uri.parse(links.get(position));
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }

Hope this helps someone.

admiralchip
  • 127
  • 1
  • 2
  • 11