Question

I'm developing an android application which takes data from Rss Feeds and displays it in a ListView. Right now I managed to successfully parse the title and display it in the ListView. Problem is that when I click on the ListView item (i.e., title) I need to load the description but I don't know how to parse it so please help me..

this is my RssItem Page:

public class RssItem {
// item title
    private String title;
    // item link
    private String link;
    private String description;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    @Override
    public String toString() {
        return title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}

This is My Rssparse Handler Page

public class RssParseHandler extends DefaultHandler {

private List<RssItem> rssItems;

// Used to reference item while parsing
private RssItem currentItem;

// Parsing title indicator
private boolean parsingTitle;
// Parsing link indicator
private boolean parsingLink;

public RssParseHandler() {
    rssItems = new ArrayList<RssItem>();
}

public List<RssItem> getItems() {
    return rssItems;
}

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    if ("item".equals(qName)) {
        currentItem = new RssItem();
    } else if ("title".equals(qName)) {
        parsingTitle = true;
    } else if ("link".equals(qName)) {
        parsingLink = true;
    }

}

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {

    if ("item".equals(qName)) {
        rssItems.add(currentItem);
        currentItem = null;
    } else if ("title".equals(qName)) {
        parsingTitle = false;
    } else if ("link".equals(qName)) {
        parsingLink = false;
    } else if ("description".equals(qName)) {

    }
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {
    if (parsingTitle) {
        if (currentItem != null)
            currentItem.setTitle(new String(ch, start, length));
    } else if (parsingLink) {
        if (currentItem != null) {
            currentItem.setLink(new String(ch, start, length));
            parsingLink = false;
        }
    } 
}

}

This page shows the description when an item is clicked

public class Openpage extends Activity {
WebView wb=null;
String s1=null;

 @SuppressLint({ "SetJavaScriptEnabled", "HandlerLeak" })
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newsfeed);
        Intent i=getIntent();
        s1=i.getStringExtra("link");

        wb=(WebView)findViewById(R.id.webView1);
        wb.loadUrl(s1);

 wb.setWebChromeClient(new WebChromeClient() {
       public void onProgressChanged(WebView view, int progress) {
       if(progress == 100){
        // Page has fully loaded. Cancel the progress dialog here
      }
    }
    });
 }
 class CustomWebViewClient extends WebViewClient {
        public boolean shouldOverrideUrlLoading(WebView view, String url) 
        {
            //do whatever you want with the url that is clicked inside the webview.
            //for example tell the webview to load that url.
            view.loadUrl(s1);
            //return true if this method handled the link event
            //or false otherwise
            return true;
        }
    }

}

Was it helpful?

Solution

that should be pretty simple, but can you post what xml\json parser are you using?

Essentially you should use an xml\json parser to get all the info from the feed, the feed itself is probably a list of items so use the parser you will select API to get every item data - essentially its no different then getting the title and name of the feed its just a simple plain text. then instantiate every listview item to a feed class and upon clicking get the info from the class.

I can post a full working code on my github profile for that later on today since I have already done that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top