开发者

Layout Parsing Xml

开发者 https://www.devze.com 2023-01-15 00:36 出处:网络
i\'m make example parsi开发者_StackOverflow社区ng xml from http://www.androidpeople.com/android-xml-parsing-tutorial-using-saxparser/

i'm make example parsi开发者_StackOverflow社区ng xml from

http://www.androidpeople.com/android-xml-parsing-tutorial-using-saxparser/

in this example, layout using an array textview..

I tried to change the layout into a ListView, but is not working..

I get error in adapter.. can anyone help me?


change your XMLParsingExample.java class

public class XMLParsingExample extends Activity {

    SitesList sitesList = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(1);
        ListView listVw = new ListView(this);
        ArrayList<String> listItm = new ArrayList<String>();

        try {

            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();
            URL sourceUrl = new URL(
                    "http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml");

            MyXMLHandler myXMLHandler = new MyXMLHandler();
            xr.setContentHandler(myXMLHandler);
            xr.parse(new InputSource(sourceUrl.openStream()));

        } catch (Exception e) {
            System.out.println("XML Pasing Excpetion = " + e);
        }
        sitesList = MyXMLHandler.sitesList;

        for (int i = 0; i < sitesList.getName().size(); i++) {
            listItm.add("Name = " + sitesList.getName().get(i));
            listItm.add("Website = " + sitesList.getWebsite().get(i));
            listItm.add("Website Category = " + sitesList.getCategory().get(i));
        }
        listVw.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, listItm));

        layout.addView(listVw);
        setContentView(layout);

    }
}

run this code and get all parsing value in listview.

0

精彩评论

暂无评论...
验证码 换一张
取 消