bind sql xml to gridview in Android

486 Views Asked by At

I've got a basic Andriod app that has a button and a gridview on it. When the button is clicked, the app calls a web service and retrieves a sql-xml string from it (C#, Dataset.GetXML()), which all works great. I need to know how to bind the GridView control to this sql:xml to display the data. I know it's got something to do with a type of Adapter but I can't figure it out.

Any help is greatly appreciated!

@lyricsboy: Here's a copy of the XML that comes back from the web service (keep in mind that this is a Web Service that I built in .NET so if any changes to that need to be made, they can):

<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="RemoteWebService">
<AllContacts> 
  <Table>
    <ContactID>7</ContactID> 
    <ContactName>Support</ContactName> 
    <EmailAddress>[email protected]</EmailAddress> 
    <CCEmail>[email protected]</CCEmail> 
    <Visible>true</Visible> 
    <Description>If you have concerns you can use this email. </Description> 
 </Table>
 <Table>
   <ContactID>8</ContactID> 
   <ContactName>Training</ContactName> 
   <EmailAddress>[email protected]</EmailAddress> 
   <CCEmail />
   <Visible>true</Visible> 
   <Description>This email is designated for Training issues.</Description>
 </Table>
</AllContacts>
</string>
1

There are 1 best solutions below

4
lyricsboy On BEST ANSWER

First you'll need to parse the XML into something that you can give to an adapter. If the data sets are of a reasonable size, you can use model objects of your own design to hold the data. Create an ArrayList of them, and pass them off to ArrayAdapter or a subclass of your own design (or your own custom Adapter if you need it).

Based on the sample XML, I would create a Contact class that had fields you could use to store all of the data you send for each item. Then write some code that uses XmlPullParser (docs) to turn your XML into Contact instances and put them in an ArrayList. Pass that list to an ArrayAdapter that you then pass to the GridView.