Picking up custom objects from dropdownlist
I am having an issue with picking up custom data types from a drop down lists. To make this as easy to understand as possible, I'll use a simple example of what I want to be able to do
So say I have a custom data type (Say of type Dog). Dog contains a name, breed and age. I store each instance of a dog in an ArrayCollection:
[Bindable]
private var dogData : ArrayCollection;
This ArrayCollection holds 1..N Dog objects with the respective information. Now having a dropdown like so:
<s:DropDownList x="81" y="178" id="dogSelected" prompt="Dog Selected:" dataProvider="{dogData}" labelField="dogNameData" />
the dogNameData would hypothetically come from a custom ActionScript class that has the 'name' field of the Dog in that object.
Now I want to select a certain dog from the dropdown. I tried to just do it this way:
var theDog : Dog;
theDog = do开发者_StackOverflow社区gSelected.selectedItem;
However, ActionScript does not seem to like this. Now, I read around and found out that using the label field is the way to be able to select this. I have been unable to select the dog item, so I can then bind it to:
var selectedDogBreed : String;
//var theDog : Dog = the selected object from my drop down
selectedDogBreed = theDog.breed
Would anyone be able to help me be able to select this object from the drop down? Much thanks in advance.
Also to note, the ArrayCollection is dynamically generated. In my actual application I am trying to figure this out for, my array of custom data is dynamic. Nothing is hard coded
To access the selectedItem of a drop down; you'll have to cast it as the type you want:
var theDog : Dog;
theDog = dogSelected.selectedItem as Dog
The labelfield has nothing to do with accessing the selectedItem. The labelField is just used by the default itemRenderer to decide what value to display in the drop down. IF you're not seeing any text displayed in the drop down; or seeing [object object] or something similar, then that is where labelField comes into play.
Have you tried?
trace( 'name ' + (dogData[dogSelected.selectedIndex] as Dog).name )
or without type casting
trace( 'name ' + dogSelected.selectedItem.name )
If this does not work then post your error codes.
精彩评论