XML解析并在列表视图中显示结果

时间:2011-08-23 07:11:47

标签: android xml-parsing listactivity

我使用Android-people-XMLParsing示例的以下代码来解析XML文件,它工作得很好..

但它检索TextView中的数据我想将此数据放入ListView ..

我尝试创建CustomAdapter并将Activity更改为ListActivity ..但没有任何作用..我只能看到一个空白的屏幕..任何人都可以帮我这样做...

谢谢!

     package com.androidpeople.xml.parsing;

     import java.net.URL;
     import javax.xml.parsers.SAXParser;
     import javax.xml.parsers.SAXParserFactory;
     import org.xml.sax.InputSource;
     import org.xml.sax.XMLReader;
     import android.app.Activity;
     import android.graphics.Color;
     import android.os.Bundle;
     import android.widget.LinearLayout;
     import android.widget.TextView;

    public class XMLParsingExample extends Activity {

/** Create Object For markerList Class */
mymarkers markerList =null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    /** Create a new layout to display the view */
    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(1);



    /** Create a new textview array to display the results */
    TextView name[];
    TextView address[];
    TextView city[];
    try {

        /** Handling XML */
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        /** Send URL to parse XML Tags */
        URL sourceUrl = new URL("xxxxxxxxx");

        /** Create handler to handle XML Tags ( extends DefaultHandler ) */
        MyXMLHandler myXMLHandler = new MyXMLHandler();
        xr.setContentHandler(myXMLHandler);
        xr.parse(new InputSource(sourceUrl.openStream()));

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

    /** Get result from MyXMLHandler SitlesList Object */
    markerList = MyXMLHandler.markerList;

    /** Assign textview array lenght by arraylist size */


    name = new TextView[markerList.getName().size()];
    address = new TextView[markerList.getaddress().size()];
    city = new TextView[markerList.getCity().size()];
    /** Set the result text in textview and add it to layout */
    for (int i = 0; i < markerList.getName().size(); i++) {
        name[i] = new TextView(this);
        name[i].setText("Name = "+markerList.getName().get(i));

        address[i] = new TextView(this);
        address[i].setText("address = "+markerList.getaddress().get(i));

        city[i] = new TextView(this);  
        city[i].setText("city = "+markerList.getCity().get(i));
        System.out.println("count33..."+markerList.getCity().size());
        city[i].setBackgroundColor(Color.WHITE);
        layout.addView(name[i]);
        layout.addView(address[i]);
        layout.addView(city[i]);

        //layout.addView(border[i]);
    }

    /** Set the layout view to display */
    setContentView(layout);

}
}

1 个答案:

答案 0 :(得分:0)

几天前我回答了一个非常类似的问题,你可能想检查一下。您可以查看问题Android: Sax parsing to a listview并查看接受的答案。按照说明直到getView()部分,你应该没问题。

总之,您需要做的是:

  • 使用DOM而不是SAX,它更简单,更适合ListView
  • 实现扩展BaseAdapter的适配器,将根元素传递给此适配器
  • 在适配器中实现getView以添加所需的TextView

我再次列出了Android: Sax parsing to a listview中的步骤。如果您有任何其他问题,请告诉我