gpt4 book ai didi

java - 如何在Android中的Web服务的 ListView 中显示数据

转载 作者:行者123 更新时间:2023-12-02 00:47:36 26 4
gpt4 key购买 nike

我的网络服务返回以下格式的数据

ab cdf我必须在 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);

}

}

检查我的代码后告诉我哪里错了并告诉我正确的答案。

最佳答案

一种解决方案是解析来自 Web 服务的 XML 响应并将解析后的数据存储在数组中。然后只需使用数组适配器来填充 ListView。

关于java - 如何在Android中的Web服务的 ListView 中显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4495770/

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