Can I initialize an Array list from data specified in xml resource file?

by mafro » Tue, 10 Mar 2009 00:41:53 GMT

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

In res/values/arrays.xml:
    <!-- Used in View/ -->
    <string-array name="colors">

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

String[] arr = res.getStringArray(R.array.colors);
for(int i=0; i<arr.length-1; 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.



Can I initialize an Array list from data specified in xml resource file?

by mafro » Tue, 10 Mar 2009 00:48:34 GMT

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

Spinner s1 = (Spinner) findViewById(;
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource
(this, R.array.colors, android.R.layout.simple_spinner_item);


Sponsored Links

Can I initialize an Array list from data specified in xml resource file?

by Videoguy » Tue, 10 Mar 2009 02:33:12 GMT

 I have two text fields in each row of ListView.
Is it possible to specify both fields in one string array of XML
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.



Can I initialize an Array list from data specified in xml resource file?

by mafro » Tue, 10 Mar 2009 21:40:37 GMT

  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"?>
<meal name="breakfast" tree="bonsai" food="egg" />
<meal name="lunch" tree="deciduous" food="ham" />
<meal name="dinner" tree="evergreen" food="steak" />

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

try {

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
//iterate the attributes adding to our HashMap
and List
for(int i=0; i<xrp.getAttributeCount(); i++) {
eventType =;

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

//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 =;
Log.i("DATA", hm.get("name") +' '+ hm.get("food") +' '+ hm.get

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(
new String[] {"name", "food"},
new int[] {,}

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/

Can I initialize an Array list from data specified in xml resource file?

by Videoguy » Thu, 12 Mar 2009 21:32:13 GMT

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

Thanks for your help!


Other Threads

1. Access drawable resources from ItemizedOverlay

if u jus need resources, have a static Context instance in ur launcher
activity to which u assign the getApplicationContext() in the onCreate
method. Now u hv access to ur app Context from any where in ur application.
"m not sure if this is the best method.
But this is wat i could think of..

Fred Allen <>  -
"California is a fine place to live - if you happen to be an orange."


2. ListView with different types of View's

Hello for all the Android's out there,

I'm trying to make a ListView when every item in that list can be a
different type of View.
for example the list could be:
1. TextView
2. ImageView
3. MyCustomView

I understands that in order to make a list I need to use an Adapter.
the problem is that the Adapter uses a single layout for all of the
list items, which will not support the different views.

my goal is to make an abstract View list when I can dynamically add
and removes items in that list (each item is a View or subclass of



3. Soft keyboard pushes stuff up - how to find new Y position (ViewDebug)?

4. Rotating map view

5. How to get a java object property from javascript on Android?

6. JIS conversion is ignored!

7. how to modify value in AttributeSet?