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. Can't delete SMS in SI

Hi all :
   I try to delete SMS in SIM from Messaging , but neither I choice
delete all or delete one SMS in SIM , the SMS can't be deleted.
  I check source code in SmsManager , I find that the function
simISms.updateMessageOnSimEf always return false , but with Android
1.5 SDK no ISms.c anymore but only ISms.aidl , so please help or
advice me the guide to solve this issue.


2. Setting Default Ringer Issue

I'm setting the default ringer to a resource in my application with
the following code.

Uri mediaUri = Uri.parse("android.resource://
com.Diligent.Apps.YoGabbaBoard/" + mediaId);
RingtoneManager.TYPE_RINGTONE, mediaUri);

I can play the resource via the RingtoneManager with this code:
RingtoneManager.getRingtone(getApplicationContext(), mediaUri).play();

This plays the sound I would expect.

However, if I ask the RingtoneManager to play the default ringer, it
plays the default android ringer.
Here is the code I'm using to play the default ringer:

Uri defaultUri = RingtoneManager.getDefaultUri
Ringtone rt = RingtoneManager.getRingtone(getApplicationContext(),

The question is...
Why is the "setActualDefaultRingtoneUri" method call not setting the
default ringer?

What am I doing wrong? I've searched many user forums, read the docs,
and all the news groups and I cant seem to find what I'm doing wrong.


3. More Appwidget Phantom Scenarios?

4. Adding your app to other apps menu how to

5. GUI test tool/framework for Android?

6. Error in running unit tests

7. font typeface dynamically