在SAX解析后在列表视图中显示数据

时间:2011-06-02 02:28:20

标签: java android xml listview sax

任何人都可以给我一个完整的教程系列链接或示例eclipse项目代码链接,用于在Android中对SAX解析XML后在列表视图中显示数据。

提前致谢。

3 个答案:

答案 0 :(得分:4)

这里试试这个

在您希望listView位于的活动中:

public class listView extends ListActivity {
    ArrayList<String> items = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listViewLayout);
        try {
            /** Handling XML */
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();

            /** If using remote server for xml file */
            URL sourceUrl = new URL("http://www.server.com/fileToParse.xml");

            /** Create handler to handle XML Tags ( extends DefaultHandler ) */
            ExampleHandler myXMLHandler = new ExampleHandler();
            xr.setContentHandler(myXMLHandler);
            /** if using remote file */
            xr.parse(new InputSource(sourceUrl.openStream()));
            /** if local file */
            xr.parse(new InputSource(getContext().getResources().getXml(R.fileToParse.MyXml));
        }catch(Exception e) {
            Log.e("XML Error "+e.toString());
        }

        /** Get result from MyXMLHandler SitlesList Object */
        sitesList = ExampleHandler.sitesList;
        for(int i = 0; i < sitesList.getName().size(); i++) {
            items.add(sitesList.getName().get(i)+" "+sitesList.getLastName().get(i));
        }

        /** Set the layout view to display */
        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
    }

    public void onListItemClick(ListView parent, View v, int position, long id) {
        /** puExtra method for if you want to pass variable to next activity */
        Intent i = new Intent(this, listViewSub.class);
        i.putExtra("name", sitesList.getName());
        startActivity(i);
    }
}

listViewLayout.xml: Can't get layout file to show so click here

ExampleHandler.java:

public class ExampleHandler extends DefaultHandler{

    Boolean currentElement = false;
    String currentValue = null;
    public static SitesList sitesList = null;

    public static SitesList getSitesList() {
    return sitesList;
    }

    public static void setSitesList(SitesList sitesList) {
    ExampleHandler.sitesList = sitesList;
    }

    /** Called when tag starts ( ex:- <name>AndroidPeople</name>
    * -- <name> )*/
    @Override
    public void startElement(String uri, String localName, String qName,
    Attributes attributes) throws SAXException {

    currentElement = true;

    if (localName.equals("Records"))
    {
    /** Start */
    sitesList = new SitesList();
    }

    }

    /** Called when tag closing ( ex:- <name>AndroidPeople</name>
    * -- </name> )*/
    @Override
    public void endElement(String uri, String localName, String qName)
    throws SAXException {

    currentElement = false;

    /** set value */
    if (localName.equalsIgnoreCase("FirstName"))
    sitesList.setName(currentValue);
    else if(localName.equalsIgnoreCase("LastName"))
    sitesList.setLastName(currentValue);
    }   

    /** Called to get tag characters ( ex:- <name>AndroidPeople</name>
    * -- to get AndroidPeople Character ) */
    @Override
    public void characters(char[] ch, int start, int length)
    throws SAXException {

    if (currentElement) {
    currentValue = new String(ch, start, length);
    currentElement = false;
    }

    }
}

SitesList.java:

public class SitesList {

/** Variables */
private ArrayList<String> name = new ArrayList<String>();
private ArrayList<String> lastName = new ArrayList<String>();

/** In Setter method default it will return arraylist
* change that to add */

public ArrayList<String> getName() {
return name;
}

public void setName(String name) {
this.name.add(name);
}

public void setLastName(String lastName) {
    this.lastName.add(lastName);
}

public ArrayList<String> getLastName() {
    return lastName;
}

}

fileToParse.xml:Click here for xml file

答案 1 :(得分:0)

在xml&amp;中声明scrollview relativelayout就像它的孩子一样。现在,您可以添加它们创建要显示(动态)向其添加数据的表行的数量&amp;将这些表行添加到relativealayout.So将获得listview的感觉。我用过这种方式&amp;对我来说非常好。

答案 2 :(得分:0)

这是您将找到的最全面的资源:

http://www.ibm.com/developerworks/opensource/library/x-android/

基本上,您需要根据已解析的数据创建适配器并将其提供给ListView。您可以扩展BaseAdapter以进行自定义,或者只使用ArrayAdapter。