Flex 4: Dynamic ComboBox/List problem [object Object_Type] even with labelField defined
I am dynamically populating a ComboBox with an HTTPservice calling an XML file. This all works dandy through the Flash Build 4 interface. Below is the ComboBox code:
<s:ComboBox id="cbSockOptions" change="cbSockOptions_changeHandler(event)"
selectedIndex="1"
enabled="true"
creationComplete="cbSockOptions_creationCompleteHandler(event)"
labelField="Symbol">
<s:AsyncListView list="{TypeUtil开发者_如何转开发ity.convertToCollection(Operation1Result2.lastResult.Company)}"/>
</s:ComboBox>
As stated, above is my code which populates the ComboBox. I would like to retrieve the seleted value from the ComboBox, but when I do I get the following output (e.g. when presented in a Label):
[object Company_Type]
I am currently using the following code to retrieve the value of the ComboBox
cbSockOptions.selectedItem;
All the reading I have done on the subject says that I must specify a 'labelField' in my ComboBox, which I do. However, I am still seeing the [object Company_Type] as an output, instead of the real value.
help! :(
The labelfunction property only applies to displaying values in the ComboBox. There is a selectedLabel property of the ComboBox, but it is not marked as bindable. If you want to use the selectedItem elsewhere, you'll have to access the properties directly.
Going from simple to complex, you could try
<s:Label text="{ cbSockOptions.selectedItem[cbSockOptions.labelField] }" />
But I think you'll get binding warnings if you do that because of the array accessor - and it is never good to cause warnings.
A better idea would be to cast the selectedItem to the same type as the items in the collection created by TypeUtility.convertToCollection, e.g.
<s:Label text="{ CompanyClassName( cbSockOptions.selectedItem ).propertyToDisplay }" />
The best idea would be to use your cbSockOptions_changeHandler to set a local variable (e.g. selectedCompany) to use for binding. If your classes is Bindable, then you could just used the selectedCompany
<s:Label text="{ selectedCompany.propertyToDisplay }" />
otherwise if the class is not bindable, then you could just use another property to store the value
<s:Label text="{ propertyToDisplay }" />
Posting this to help people with a similar issue. I had an [object Object] issue on my spark Lists and they would show using static data just fine, but not dynamic collections pulled from the server. After countless deadends I started fiddling. I was using custom skins and item renders so I, used the standard spark list and it worked. I added my custom item render to the standard spark list, and got [object Object] again. So i determined the issue was with my custom item render.
And the solution... By default Flash Builder 4 makes the base Item Renders grab the string info from "{data}" object. Simply change that to "{label}" in your item renderer to be able to use the labelFunction with complex objects and custom skins.
精彩评论