I had a little tinker with this idea this morning, and you could
implement it by using an external XML resource. Create your data in
res/xml/ - mine was called meals.xml. Dont ask why ;)

<?xml version="1.0" encoding="utf-8"?>
<meals>
        <meal name="breakfast" tree="bonsai" food="egg" />
        <meal name="lunch" tree="deciduous" food="ham" />
        <meal name="dinner" tree="evergreen" food="steak" />
</meals>

And then code to retrieve and parse this is like the following:

Resources res = this.getResources();

//get arbitary xml data via parser
XmlResourceParser xrp = res.getXml(R.xml.meals);

//create ArrayList of HashMaps for our data
ArrayList<HashMap<String,String>> data = new
ArrayList<HashMap<String,String>>();

try {
        //from http://d.android.com/reference/org/xmlpull/v1/XmlPullParser.html

        int eventType = xrp.getEventType();

        while (eventType != XmlPullParser.END_DOCUMENT) {
                if(eventType == XmlPullParser.START_TAG) {
                        if(xrp.getName().equals("meal")) {
                                //every START_TAG event means a new item in our 
source XML
                                HashMap<String,String> item = new 
HashMap<String,String>();
                                //iterate the attributes adding to our HashMap 
and List
                                for(int i=0; i<xrp.getAttributeCount(); i++) {
                                        item.put(xrp.getAttributeName(i), 
xrp.getAttributeValue(i));
                                }
                                data.add(item);
                        }
                }
                eventType = xrp.next();
        }

}catch(IOException ioe) {
        Log.e("Exception", ioe.getStackTrace().toString());
}catch(XmlPullParserException xppe) {
        Log.e("Exception", xppe.getStackTrace().toString());
}finally{
        xrp.close();
}

//let's log our HashMap contents out to ensure XML parsing worked ok
ListIterator<HashMap<String,String>> li = data.listIterator();
while(li.hasNext()) {
        HashMap<String,String> hm = li.next();
        Log.i("DATA", hm.get("name") +' '+ hm.get("food") +' '+ hm.get
("tree"));
}

Have a look through this code and post again if you need to ask
another question.

Next, to do your binding with this data, you would use a SimpleAdapter
- quote from the docs:

"An easy adapter to map static data to views defined in an XML file.
You can specify the data backing the list as an ArrayList of Maps.
Each entry in the ArrayList corresponds to one row in the list. The
Maps contain the data for each row. You also specify an XML file that
defines the views used to display the row, and a mapping from keys in
the Map to specific views."

SimpleAdapter sa = new SimpleAdapter(
        this,
        data,
        R.layout.meal_item,
        new String[] {"name", "food"},
        new int[] {R.id.meal, R.id.food}
);
setListAdapter(sa);

Here the ArrayList<HashMap<String,String>> is supplied as our data
source, R.layout.meal_item is given as our UI list item and the final
two params are the binding key/value pairs.

I hope this is clear enough! Looks like the external XML resource
thing is there to give you the flexibility to achieve pretty much
anything. The <string-array> construct is just a convenience.

mafro


On Mar 9, 6:32 pm, Videoguy <[email protected]> wrote:
> I have two text fields in each row of ListView.
> Is it possible to specify both fields in one string array of XML
> resource?
> All the examples I have seen map 1dimensional array like above to
> ListView rows.
>
> Can I specify an array of arrays in an xml?
>
> I appreciate your help.
>
> Videoguy
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to