0
我的web服務的web服務列表視圖中的數據格式返回數據如何顯示在Android的
AB CDEF 我必須展示在這裏的ListView這回的數據是我的welcome.xml在我所顯示數據
的welcome.xml
<?xml version="1.0" encoding="utf-8" ?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical">
<ImageView android:id="@+id/welcome" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/main" android:layout_gravity="center_horizontal" />
-
- <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:id="@+id/Relative01">
<ListView android:id="@+id/lvevent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@+id/welcome" android:background="@drawable/wel_bg" android:layout_marginBottom="50dip" />
</RelativeLayout>
- <TableLayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_below="@+id/Relative01">
- <TableRow>
<Button android:id="@+id/btn_index" android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/cap_button" android:paddingRight="15dip" android:layout_marginRight="50dip" />
<Button android:id="@+id/btn_event" android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/button" android:layout_toRightOf="@+id/btn_index" android:paddingLeft="25dip" android:layout_marginLeft="30dip" />
</TableRow>
</TableLayout>
</LinearLayout>
如何在listview中顯示數據?
下面的代碼我使用的解析器:
MYXMLHandler.java:
package org.parsing;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class MyXMLHandler 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) {
MyXMLHandler.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("content"))
{
/** Start */
sitesList = new SitesList();
} /*else if (localName.equals("website")) {
*//** Get attribute value *//*
String attr = attributes.getValue("category");
sitesList.setCategory(attr);
}*/
}
/** 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("title"))
sitesList.setTitle(currentValue);
else if (localName.equalsIgnoreCase("description"))
sitesList.setDescription(currentValue);
}
/** Called to get tag characters (ex:- <name>AndroidPeople</name>
* -- to get AndroidPeople Character) */
public void characters(char[] ch, int start, int length)
throws SAXException {
if (currentElement) {
currentValue = new String(ch, start, length);
currentElement = false;
}
}
}
SitesList.java:
package org.parsing;
import java.util.ArrayList;
public class SitesList {
/** Variables */
private ArrayList<String> title = new ArrayList<String>();
private ArrayList<String> description = new ArrayList<String>();
//private ArrayList<String> category = new ArrayList<String>();
/** In Setter method default it will return arraylist
* change that to add */
public ArrayList<String> getTitle() {
return title;
}
public void setTitle(String title) {
this.title.add(title);
}
public ArrayList<String> getDescription() {
return description;
}
public void setDescription(String description) {
this.description.add(description);
}
/*public ArrayList<String> getCategory() {
return category;
}
public void setCategory(String category) {
this.category.add(category);
}
*/
}
XMLParsingExample.java:
package org.parsing;
import java.net.URL;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
public class XMLParsingExample extends Activity {
/** Create Object For SiteList Class */
SitesList sitesList = 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 title[];
TextView description[];
//TextView category[];
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(
"http://208.109.97.179:1010/webservices/newsevents.php");
/** Create handler to handle XML Tags (extends DefaultHandler) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler((ContentHandler) myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/** Get result from MyXMLHandler SitlesList Object */
sitesList = MyXMLHandler.sitesList;
/** Assign textview array lenght by arraylist size */
title = new TextView[sitesList.getTitle().size()];
description = new TextView[sitesList.getDescription().size()];
//category = new TextView[sitesList.getName().size()];
/** Set the result text in textview and add it to layout */
for (int i = 0; i < sitesList.getTitle().size(); i++) {
title[i] = new TextView(this);
title[i].setText("Title = "+sitesList.getTitle().get(i));
description[i] = new TextView(this);
description[i].setText("Website = "+sitesList.getDescription().get(i));
//category[i] = new TextView(this);
//category[i].setText("Website Category = "+sitesList.getCategory().get(i));
layout.addView(title[i]);
layout.addView(description[i]);
//layout.addView(category[i]);
}
/** Set the layout view to display */
setContentView(layout);
}
}
後CH ecking我的代碼告訴我我錯在哪裏,並告訴正確的答案。
謝謝你的答案,你可以看到我已經解析了我的數據。所以給我想法如何將它們存儲在數組中並顯示在適配器中 – sachin 2010-12-21 11:10:00
這很簡單。使用這樣的東西; setListAdapter(new ArrayAdapter(getBaseContext(),R.layout.bulleted_list_item,R.id.listItemDisplay,yourStringArrayName));請注意,這裏我使用了一個String數組,所以您應該根據您使用的數據類型進行更改。有任何懷疑還原。 –
Mudassir
2010-12-21 11:45:02