array 与 arrayCollection

本文探讨了Array与ArrayCollection在Flex中的应用差异,特别是作为数据源时的行为不同。通过实例演示了如何使用这两种数据结构,并解释了ArrayCollection为何能更好地支持数据绑定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

arrayCollection比array高级。增加了一些事件和方法。

而array没有任何事件。所以如果让他们都作为数据源的话,比如:http://tonylian.iteye.com/blog/288964 所谈到的

 

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

 为什么myArray改变了,视图没有更新?

那是因为array没有事件,它的数据的变化,不会发出任何事件。所以list就没有收到任何消息,也就不会作出改变。

除非你改变myArray指向的对象,这样list就会得到通知,而作出改变。

也就是这样:

public function addCountryToArray(country:String):void{ 
                  myArray=New Array("北京","上海","深圳","广州");} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值