繁体   English   中英

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

[英]how to show data in listview from web service in 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>

我如何在列表视图中显示数据。

我用于解析器的以下代码:

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。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM