If you notice the selectedIndex of an mx:List or anything list based for that matter, be it a, tree, combobox, or anything that is built of the base List… whenever the dataprovider changes the selectedIndex is lost…. say i want to bind my

<mx:List id=“myList” selectedIndex=“{customClass.someID}”>


that way i could have some values in that list, for example
price ranges:
100k – 200k
200k – 300k < this is selected for this house
300k – 400k

but in a different city i have

price ranges:
150k – 250k < this is selected for this house
250k – 400k
400k – 500k

so i’m binding to a custom class that i made, that grabs the current price range of the house… but whenever my dataprovider changes, the selected index is lost…

CLICK HERE TO SEE THE BAD EXAMPLE

it is expected, but i just don’t know how to fix it…. how do you set something selected in a component after you receive new data?

but i figured it out…

thanks to a post by ben forta… CLICK HERE FOR BEN’S POSTthe post wasn’t what i wanted or needed, but there is no fun when you don’t have to try….

so what you have to do is extend the List, or ComboBox components….

create a new component based on the list or combo box… and in that component put the following code…

<?xml version=“1.0″ encoding=“utf-8″?>
<mx:List xmlns:mx=http://www.adobe.com/2006/mxml>
<mx:Script>
<![CDATA[
private var tempSelectedIndex:Number = -1;
private var bDataProviderSet:Boolean = false;

// Trap dataProvider being set override public function set dataProvider(o:Object):void{
tempSelectedIndex = this.selectedIndex;
super.dataProvider = o;
// This may get called before dataProvider is set, so make sure not null and has entries if (o!=null && o.length){
// Got it, set flag bDataProviderSet = true;
}
this.selectedIndex = tempSelectedIndex;
}

]]>
</mx:Script>
</mx:List>

CLICK HERE TO SEE THE GOOD EXAMPLE

Just right click to view the source!