Array 和 ArrayCollection 区别

 ArrayCollection 实现接口 ICollectionView,在 Flex 的类定义内属于[数据集],他提供更强大的检索、过滤、排序、分类、更新监控等功能。FDK2提供的类似的类还有 XMLListCollection

      这两者差别在于如果用 array 在作为 data provider 绑定于 control 之上,就无法获得控件的更新,除非控件被重新绘制或者 data provider 被重新指定,而 Collection 则是将 array 的副本存储于 Collection 类的某个对象之中,其特点是 Collection 类本身就具备了确保数据同步的方法,例子如下(取自 adobe 内部工程师 training 示例,稍有改变)

 

<?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>

 

 

 

ArrayCollection的几个使用技巧:

var arr:Array = new Array();

var ac:ArrayCollection = new ArrayCollection();

ac.disableAutoUpdate();
arr.shift();
arr.push(new Object());
ac.enableAutoUpdate();
ac.source = arr;//某些情况下,更改数据源AC不能触发页面刷新,但是更改数据源ac的source却可以。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值