我使用下面的代碼用Android的人XMLParsing例如用於解析XML文件,它是工作完全正常..XML解析,並顯示在列表視圖中的結果
但檢索的TextView數據我想把這個數據在一個ListView ..
我嘗試通過創建CustomAdapter並通過將活動更改爲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);
}
}
我認爲你需要先學習ListView的ListView和自定義adpater。那麼你很容易實現這一點。 –