Skip to content Skip to sidebar Skip to footer

Android: Sax Parsing To A Listview

First off I'm new to Android and Java.. I have been trying to add a 'simple' Rss Reader to an app of mine. All I want it to do is Parse a RSS feed from a specific site and take the

Solution 1:

Let's take this step by step and hopefully we could achieve what you want to do. I have included relevant links to the API Documentation that you need to know.

  • For ListView, DOM is probably a simpler choice since ListView is operating on a list which would work nicely with structured list. It's also easier to learn for someone who new to Android and Java. Look at this IBM tutorial for XML on section "DOM-based implementation of feed parser". Below is the section of the code you should care for in the example
DocumentBuilderbuilder= factory.newDocumentBuilder();
    Documentdom= builder.parse(this.getInputStream());
    Elementroot= dom.getDocumentElement();

  • At this point you have the data for your ListView. ListView works by taking an Adapter. Adapter is the one responsible for a. Setting the initial data and b. Showing how each row should look like. As such this class is important for making ListView works
  • Now you are going to create your own extension of BaseAdapter which will take the root element that you have gotten before. I have provided a skeleton class here:

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

publicclassRSSAdapterextendsBaseAdapter {
    Element rootElement;

    publicRSSAdapter(Element rootElement) {
        this.rootElement = rootElement;
    }

    publicintgetCount() {
        // get the count of children of the root elementreturn0;
    }

    public Object getItem(int position) {
        // get the child identified by the position, // this is the important part where you    // want to get the right child representing the RSS data that you want to getreturnnull;
    }

    publiclonggetItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // implement your view here....returnnull;
    }
}
Copy

  • If you are not familiar with Element, I suggest you look at the following Element API Documentation. You would need to correctly identify the child in getItem() function above
  • Now you are ready to implement getView() method. Here you would basically do the following step
    • Get access to the child node that you want using getItem() that you have implemented
    • Create an XML layout for each row and inflate them here. If you are not familiar with Android View, look at View API Documentation as this is an important thing to know
    • Extract the data from the child node and set it to your corresponding View element
  • Next, you need to set up OnItemClickListener on your ListView. In the listener you would want to get the XML again using getItem() (this method is crucial for you) and get the link.
  • Finally, in the implementation of onClick(), you want to use WebView and load the URL

This should be enough for you to implement what you want. I would clarify further if the steps are not clear. Hope this helps.

Post a Comment for "Android: Sax Parsing To A Listview"