Array 和 ArrayCollection

          今天做东西,就又绕到一个很原始的问题,Array 和 ArrayCollection有什么区别,在网上找到了这样一个例子,很有帮助,先贴出来,以后再有新的理解,就再说呗。

          原文地址:http://hi.baidu.com/tokios/blog/item/aeb5cd1b4b974bfeaf513384.html

 

 

 

 

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

这两者差别在于如果用array在作为data provider绑定于控件上,就无法获得控件的更新,除非控件被重新绘制或者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;   
              public function addCountryToArra(country:String):void{   
                  myArray.push(country);   
              }   
              public function addCountryToCollection(country:String):void{
              	  myCollection.source=myArray;   
                  myCollection.addItem(country);   
              }   
          ]]>    
      </mx:Script>    
      <mx:TextInput id="countryTextInput" text="广州"/>    
      <mx:Label text="Bound to Array (Raw Object)"/>    
      <mx:Button click="addCountryToArra(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>

 

 

 

 总结:

 

当Array的数据发生变化的时候,用它作为数据源的控件不能感知这种变化。
例如:myArray.push("new"); 这时,如果一个List用它作为dataProvider,List的列表中不会增加新加入的这个值。如要改变,得重新给控件赋值。


而当ArrayCollection的数据发生变化的时候,能够通知控件发生变化。
例如:myArrayCollection.addItem("new"); 这时,如果一个控件List用它作为dataProvider,List列表中会增加一列内容.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xaajie

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值