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:

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

In res/values/arrays.xml:
<resources>
    <!-- Used in View/Spinner1.java -->
    <string-array name="colors">
        <item>red</item>
        <item>orange</item>
        <item>yellow</item>
        <item>green</item>
        <item>blue</item>
        <item>violet</item>
    </string-array>
</resources>

//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.

mafro





--~--~---------~--~----~------------~-------~--~----~



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/
android/apis/view/Spinner1.java:

Spinner s1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> 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);





--~--~---------~--~----~------------~-------~--~----~


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

--~--~---------~--~----~------------~-------~--~----~



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"?>
<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/



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!

Videoguy
--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Fw:porting android to omap3 evm

Hi Cindy,

1.what do you mean about cupcake branch
Cupcake is the new android branch which has just been merged to master
branch.
Means to say that it is the latest android code drop branch.
So In that currently alsa (for audio support ) is breaking, but first of all
ur basic android should boot.

2.Boot from nfs

Try with the following nfs bootargs :-

setenv bootargs mem=128M console=ttyS0,115200n8 noinitrd rw root=/dev/nfs
nfsroot=<path to ur filesystem on ur host machine>
,nolock,wsize=1024,rsize=1024 ip=dhcp init=/init

Please make sure you have run all the services like nfs-server etc on ur
host machine
and made a entry in ur /etc/exports

Hope it helps.

Regards
Nimit


2009/4/14 cindy_king_1024 <cindy_king_1...@126.com>


--~--~---------~--~----~------------~-------~--~----~
unsubscribe: android-porting+unsubscr...@googlegroups.com
website: 

2. Who is attending Google I/O?

If you are attending this years Google I/O Developers Conference you
can connect with others attending here:

http://droidin.com/events/google-io-developers

I thought it would be good to keep in-touch with the attendees before
and after the conference.

If you are attending, what do you plan on getting out of it?  Tips &
tricks, networking, learn more about the Android platform, just going
for fun?
--~--~---------~--~----~------------~-------~--~----~

3. ddms not running on vista 64bit

4. Eclipse android setup, cannot for the life of me!

5. Overlaying ImageViews

6. Eclipse android setup, cannot for the life of me!

7. Emulator not starting, NAND could not write error