简体   繁体   中英

how to show data in listview from web service in android

my webservice return a data in the format

ab cdef i have to show to this return data in listview here is my welcome.xml in which i have to display data

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>

how can i show data in listview.

following code i am using for parser:

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);

 }

}

after checking my code tell me where i am wrong and tell the correct answer.

One solution can be, parse the XML response from web service and store the parsed data in array(s). Then simply use array adapter to populate the ListView.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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