基于ListBase的flex组件从 data provider 获取数据。dataProvider提供了复杂数据类型,如Array,ArrayCollection,XML,XMLCollection等。dataProvider可以用mxml及AS两种方式定义。下面例子中的四个List展现同样的内容:
<?xmlversion="1.0"encoding="utf-8"?>
<mx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml"layout="horizontal">
<mx:Script>
<![CDATA[
[Bindable]
privatevarmyArray:Array=["a","b","c"];

[Bindable]
privatevarxml:XML=
<nodes>
<nodelabel="a"/>
<nodelabel="b"/>
<nodelabel="c"/>
</nodes>;
]]>
</mx:Script>

<mx:ListdataProvider="{myArray}"width="50"height="200"/>
<mx:ListdataProvider="{xml.children()}"labelField="@label"width="50"height="200"/>
<mx:Listwidth="50"height="200">
<mx:dataProvider>
<mx:Objectlabel="a"/>
<mx:Objectlabel="b"/>
<mx:Objectlabel="c"/>
</mx:dataProvider>
</mx:List>
<mx:Listwidth="50"height="200"labelField="@label">
<mx:dataProvider>
<mx:XMLList>
<nodelabel="a"/>
<nodelabel="b"/>
<nodelabel="c"/>
</mx:XMLList>

</mx:dataProvider>
</mx:List>

</mx:Application>
如果使用Collection,可以保证数据同步并提供简单数据操作,比如排序和过滤。下面的例子演示了同步,排序和过滤:
<?xmlversion="1.0"encoding="utf-8"?>
<mx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml"layout="horizontal"creationComplete="init()">
<mx:Script>
<![CDATA[
importmx.collections.SortField;
importmx.collections.Sort;
importmx.collections.ArrayCollection;
[Bindable]
//privatevarmyArray:Array=["c","b","a"];
privatevarmyArray:ArrayCollection=newArrayCollection(["c","b","a"]);
privatefunctionsort(){
varsort:Sort=newSort();
sort.fields=[newSortField()];
myArray.sort=sort;
myArray.refresh();
myArray.sort.fields=newArray();
}

privatefunctionfilter(item:Object):Boolean{
if("a"==item)
returnfalse;
else
returntrue;
}
]]>
</mx:Script>

<mx:ListdataProvider="{myArray}"width="50"height="200"/>
<!--<mx:Buttonlabel="add"click="myArray.push(myArray.length)"/>-->
<mx:Buttonlabel="add"click="myArray.addItem(myArray.length)"/>
<mx:Buttonlabel="sort"click="sort()"/>
<mx:Buttonlabel="filter"click="myArray.filterFunction=filter;myArray.refresh();"/>
</mx:Application>







































如果使用Collection,可以保证数据同步并提供简单数据操作,比如排序和过滤。下面的例子演示了同步,排序和过滤:

































