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




Cool! Thanks for that. Exactly what I need. Cheers!
its really cool, you can get professional help at my site
http://concealer.mybrute.com
Check out the cool mini-game
Thanks a lot Matt!!!
Outstanding! Working on a shopping cart and this is just what I needed. Thanks for putting it together.
Perfect. http://www.doughughes.net rocks.
All the links are down (Not Working)!