flex中Array和ArrayCollection简介

1、 Array ArrayCollection的区别(下面转载自:天梯梦

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

       这两者差别在于如果将array绑定到control组件为dataProvider,当array变化时无法自动更新控件,除非控件被重新绘制或者dataProvider被重新指定。而 ArrayCollection则是将 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>

  

 2、遍历

(1)遍历ArrayCollection

var result:ArrayCollection = listEvent.result as ArrayCollection;
	//取一个属性值
	for(var i:int = 0; i < result.length; i++){
		//第一种取属性方法
//		var name:String = result[i].confName;
		//第二种取属性方法		
//		var name:String = result.getItemAt(i).confName;
		//第三种取属性方法
		var obj:Object = result[i];
		var name:String = obj["confName"];			
		Alert.show("第三种:" + name);
}

 (2)遍历array

‍//Array:

for(var i:int=0;i<result.data.length;i++){
     var o:Object = result.data[i];
     trace(o["Symbol"],o["FinalPrice"],o["RunDate"]);
}

 

你可能感兴趣的:(arrayCollection)