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. Webview Email

Hi I am creating an app for the android using webview. I am trying to
send out information from my app using this form. It sends out
information from a variable in my html file in the webview.


<FORM action="" method="post" enctype="text/
        <input type="hidden" id="myInput" name="Joke" type="text" />
        <input type="submit" style ="width:100%;height=30%" name=""
value="Email this Joke">

It works fine on the chrome browser. It opens up windows mail on vista
when I click the submit button. It shows the information needed too.
How do I do the same thing using webview in my app to open and send
the info using the gmail app on android.

You get the application free with response. Very cool App.


2. Phone locked me out. Cannot access it at all.

Today I was showing my HTC Dream to a friend, how the lock screen
works. I pressed the "forgot pattern" button, thinking I could go back
or just enter my credentials. Since that moment, I haven't been able
to access my phone at all. I can't go back to the pattern screen and I
can't log in. It just says Invalid username or password, no matter
what I do. My e-mail is and I've tried and just ryanriopel and none of it works. I
know the password is correct, but I can't login. I tried this on both
WiFi and Edge, and I've soft reset the phone several times now.



3. android locked me out

4. DataBase connection

5. Insert a List into a Widget

6. avoid sending intent when some are already pending

7. How to fake a touch event