gpt4 book ai didi

java - SAX 解析后在 ListView 中显示数据

转载 作者:行者123 更新时间:2023-12-01 19:16:57 24 4
gpt4 key购买 nike

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

提前致谢。

最佳答案

这里试试这个

在您希望 listView 位于的 Activity 中:

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

关于java - SAX 解析后在 ListView 中显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6209793/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com