集合对象包括 Array 或 XMList 对象等数据对象,以及一系列用于访问、排序、过滤和修改数据对象中的数据项的方法。有若干个称为数据提供程序控件的 Adobe Flex 控件具有 dataProvider 属性,可以使用集合填充该属性。
下面的简单示例介绍了控件是如何定义数据提供程序(定义为 ActionScript ArrayCollection)以及使用数据提供程序的:
<!-- Simple example to demonstrate the Spark ComboBox control -->
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
public var complexDP:ArrayCollection = new ArrayCollection(
[ {ingredient:"Salmon", category:"Meat"},
{ingredient:"Potato", category:"Starch"},
{ingredient:"Cucumber", category:"Vegetable"},
{ingredient:"Steak", category:"Meat"},
{ingredient:"Rice", category:"Starch"},
{ingredient:"Cumin", category:"Spice"}
]
);
<!-- Function to handel custom input strings -->
private function myLabelToItemFunction(input:String):*
{
<!-- Returns object that matches items in dataProvider -->
return {ingredient:input, category:"mystery"};
}
]]>
</fx:Script>
<s:Panel title="Spark ComboBox Example" width="75%" height="75%">
<s:layout>
<s:VerticalLayout paddingTop="10" paddingLeft="10"/>
</s:layout>
<!-- Label that displayed current property values -->
<s:Label text="Index : {cb.selectedIndex}
Item : {cb.selectedItem.ingredient}
Type : {cb.selectedItem.category}"/>
<!-- ComboBox with custom labelToItem function -->
<s:ComboBox
id="cb"
dataProvider="{complexDP}"
width="150"
labelToItemFunction="{myLabelToItemFunction}"
selectedIndex="0"
labelField="ingredient"/>
</s:Panel>
</s:Application>
有关数据提供程序和集合的更多信息,请参阅数据提供程序和集合。