ArrayCollection or XML list of States and Countries


Seems like I’m always trying to put my hands on an XML list of states or list of countries for some Flex project or another. I’m posting these here in hopes that I’m not the only one looking for this information. I used to have this xml posted on a server somewhere and I’d just point a service at it, but now I have no idea where I posted it.

I’m also posting both of these lists as ArrayCollections. I’ve found this method the easiest and most reusable. I just instantiate the class and bind the view to it, for example:

The StatesArrayCollection looks like this:

<mx:ArrayCollection
    
xmlns:mx="http:/www.adobe.com/2006/mxml">
   <mx:source>
   <mx:Array>
       <mx:Object name="ALABAMA" abbreviation="AL" />
       <mx:Object name="ALASKA" abbreviation="AK" />
       <mx:Object name="ARIZONA" abbreviation="AZ" />
       <mx:Object name="ARKANSAS" abbreviation="AR" />
      

  </mx:Array>
  </mx:source>
</mx:ArrayCollection>

And instantiate it where ever I need it.

[Bindable]
protected var statesArrayCollection:StatesArrayCollection=new StatesArrayCollection();

I’ve also added a quick look up on abbreviation:

/**
* Given a state abbreviation this method returns the correct index.
*
* @param abbreviation String
* @return int
* @default Sweet Home Alabama
*/
public function getIndexByAbbreviation(abbreviation:String):int
{
   
for (var i:int=0; i<this.length; i++)
   
{
      
if(this.getItemAt(i).abbreviation == abbreviation)
    
{
      
return i
     
}
   
}
   
return 0;
}

{
if(this.getItemAt(i).abbreviation == abbreviation)
{
return i
}
}
return 0;
}

 
So a ComboBox might look like:

dataProvider="{statesArrayCollection}"
labelField="abbreviation"
selectedIndex="{statesArrayCollection.getIndexByAbbreviation('AL')}"/>
<mx:ComboBox id="bestStateInTheUnion"
      
dataProvider="{statesArrayCollection}"
      
labelField="abbreviation"
      
selectedIndex="{statesArrayCollection.getIndexByAbbreviation(‘AL’)}"/>

 
I don’t mind the fact that this information is embed in the application. For the most part this information is static. Now it’s possible that Texas will decide to secede from the Union. In which case I’ll have to update this list and each application I’ve used it in.
Come on Governor Perry, I don’t need those extra hours.
Do you guys find this helpful? Do you have any utility classes like this that you find yourself using for all of your projects? Should I add something like this in a ‘util’ folder in the FireStarter application? Thanks for any comments!
Sincerely,
Matt
States XML
StatesArrayCollection
Countries XML
CountriesArrayColleciton
 

This entry was posted by admin on Sunday, May 3rd, 2009 at 12:00 am and is filed under Flex. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

7 Comments

  1. Alexander Sokolov says:

    Cool! Thanks for that. Exactly what I need. Cheers!

  2. mgrandi says:

    its really cool, you can get professional help at my site

  3. Marcus says:

    http://concealer.mybrute.com
    Check out the cool mini-game

  4. Fawaz says:

    Thanks a lot Matt!!!

  5. Outstanding! Working on a shopping cart and this is just what I needed. Thanks for putting it together.

  6. Mohamed Atia says:

    All the links are down (Not Working)!

Leave a Reply