[android-developers] Re: Can I initialize an Array list from data specified in xml resource file?

2009-03-12 Thread Videoguy

Thanks Mafro. That worked. I used DocumentBuilder to parse xml from
raw folder instead of using compiled resource.

Thanks for 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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~--~~~~--~~--~--~---



[android-developers] Re: Can I initialize an Array list from data specified in xml resource file?

2009-03-10 Thread mafro

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








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> data = new
ArrayList>();

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 item = new 
HashMap();
//iterate the attributes adding to our HashMap 
and List
for(int i=0; i> li = data.listIterator();
while(li.hasNext()) {
HashMap 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> 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  construct is just a convenience.

mafro


On Mar 9, 6:32 pm, Videoguy  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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~--~~~~--~~--~--~---



[android-developers] Re: Can I initialize an Array list from data specified in xml resource file?

2009-03-09 Thread Videoguy

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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~--~~~~--~~--~--~---



[android-developers] Re: Can I initialize an Array list from data specified in xml resource file?

2009-03-09 Thread mafro

More to the original question.. You might just want to skip the
ArrayList and do something like this, from ApiDemos/src/com/example/
android/apis/view/Spinner1.java:

Spinner s1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter adapter = ArrayAdapter.createFromResource
(this, R.array.colors, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource
(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);


On Mar 9, 4:31 pm, mafro  wrote:
> Taking an example from the Dev Guide - you can do it like this:
>
> http://d.android.com/guide/samples/ApiDemos/res/values/arrays.html
>
> In res/values/arrays.xml:
> 
>     
>     
>         red
>         orange
>         yellow
>         green
>         blue
>         violet
>     
> 
>
> //java code
> Resources res = this.getResources();
>
> String[] arr = res.getStringArray(R.array.colors);
> for(int i=0; i         Log.i("TEST", arr[i]);
>
> }
>
> So R.array.colors is your reference. You can create an ArrayList and
> add this array to it if necessary.
>
> mafro
>
> Videoguy wrote:
> > Is is possible to specify data in xml resource file and initialize
> > ArrayList from it?
> > I am looking for a way to use the ArrayList with a SimpleAdapter to
> > bind to a ListView.
> > I am wondering if there is a way to specify my data in xml resource
> > file and init ArrayList from it.
>
> > Videoguy
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~--~~~~--~~--~--~---



[android-developers] Re: Can I initialize an Array list from data specified in xml resource file?

2009-03-09 Thread mafro

Taking an example from the Dev Guide - you can do it like this:

http://d.android.com/guide/samples/ApiDemos/res/values/arrays.html

In res/values/arrays.xml:



red
orange
yellow
green
blue
violet



//java code
Resources res = this.getResources();

String[] arr = res.getStringArray(R.array.colors);
for(int i=0; i Is is possible to specify data in xml resource file and initialize
> ArrayList from it?
> I am looking for a way to use the ArrayList with a SimpleAdapter to
> bind to a ListView.
> I am wondering if there is a way to specify my data in xml resource
> file and init ArrayList from it.
>
> Videoguy

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~--~~~~--~~--~--~---