XML解析,并显示在列表视图中的结果
问题描述:
我使用下面的代码用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);
}
}
答
我几天前回答了一个非常类似的问题,您可能需要检查。您可以检查问题Android: Sax parsing to a listview并检查接受的答案。按照指示,直到getView()部分,你应该没问题。
在这里总之就是你需要做什么:
- 使用DOM而非SAX,它是ListView控件
- 简单,完美实现一个可扩展BaseAdapter通过根元素到该适配器的适配器
- 在适配器中实现getView以添加所需的TextView
我再次在Android: Sax parsing to a listview中规划了步骤。让我知道如果您有任何进一步的问题
+0
你的回答基本上总结,帮助我得到理想的输出....谢谢.. –
我认为你需要先学习ListView的ListView和自定义adpater。那么你很容易实现这一点。 –