Flex中Array,ArrayList,ArrayCollection的區別

來源:互聯網
上載者:User

from:http://bbs.airia.cn/forum-viewthread-tid-44906-highlight.html

當Array的資料發生變化的時候,用它作為資料來源的控制項不能感知這種變化。
例如:myArray.push(“new value”); 這時,如果一個List用它作為dataProvider,List的列表中不會增加新加入的這個值。
而當ArrayCollection的資料發生變化的時候,能夠通知控制項發生變化。
例如:myArrayCollection.addItem(“new item”); 這時,如果一個控制項List用它作為dataProvider,List列表中會增加一列內容。
=================================================================
ArrayCollection實現介面ICollectionView,在Flex的類定義內屬於[資料集],他提供更強大的檢索、過濾、排序、分類、更新監控等功能。FDK2提供的類似的類還有XMLListCollection.
這兩者差別在於如果用array在作為data provider綁定於control之上,就無法獲得控制項的更新,除非控制項被重新繪製或者data provider被重新指定,而Collection則是將array的副本儲存於Collection類的某個對象之中,其特點是Collection 類本身就具備了確保資料同步的方法
在Flex 4中的引入了一種新資料類型:ArrayList。它實現了IList介面,底層以Array作為資料來源。本質上它是一種輕量級的ArrayCollection,可以作為資料來源綁定到控制項上。但與ArrayCollection不同的是,它不能篩選和過濾資料。注意,基於list的Spark控制項是不能直接以Array作為資料提供者的,一定要實現IList介面。這也應該是引入ArrayList資料類型的原因之一

<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"> <mx:Script> <![CDATA[ import mx.collections.ArrayCollection; [Bindable] public var myArray:Array=["北京","上海","深圳"]; [Bindable] public var myCollection:ArrayCollection=new ArrayCollection(myArray); public function addCountryToArray(country:String):void{ myArray.push(country); } public function addCountryToCollection(country:String):void{ myCollection.addItem(country); } ]]> </mx:Script> <mx:TextInput id="countryTextInput" text="廣州"/> <mx:Label text="Bound to Array (Raw Object)"/> <mx:Button click="addCountryToArray(countryTextInput.text)" label="Add Country to Array"/> <mx:List dataProvider="{myArray}" width="200"/> <mx:Label text="Bound to Collection"/> <mx:Button click="addCountryToCollection(countryTextInput.text)" label="Add Country to Collection"/> <mx:List dataProvider="{myCollection}" width="200"/> </mx:Application>

ArrayList的使用:

<?xml version="1.0" encoding="utf-8"?><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/mx" minWidth="955" minHeight="600"><fx:Declarations><s:ArrayList id="al"><fx:Object type="Line" class="LineSeries"/><fx:Object type="Mountain" class="AreaSeries"/><fx:Object type="Candlestick" class="CandlestickSeries"/><fx:Object type="HLOC" class="HLOCSeries"/></s:ArrayList></fx:Declarations><s:DropDownList dataProvider="{al}" labelField="type" selectedIndex="0"/></s:Application>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.