As such, an Array would appear to satisfy the data provider needs of any list-based control. However, it does not. Incompatibility with data binding makes an Array a poor choice of data provider. Fortunately, Flex provides a suitable alternative - ArrayCollection. An ArrayCollection provides all the features of an Array plus more.
To understand an ArrayCollection, one must first understand an Array.
Array
From Wikipedia, an Array "is a data structure consisting of a group of elements that are accessed via indexing". Arrays can store simple data types (primitives) such as number, boolean or string or complex data types such as other Arrays or objects.In fact, Arrays defined in ActionScript (or MXML) are not strongly-typed and can store various types of data.
private var myArray:Array = new Array(); |
// retrieving the value of an array element |
var myObject:Object= new Object(); |
var myObject:Object= new Object(); |
// retrieving the value of an object via an named index using dot notation |
// adds an element to the end of an array and increases the array length by one |
Method #1 – Create an Array by defining (or not defining) the number of elements
// creates an array with zero elements (this is the default when no argument is passed |
private var myArray:Array = new Array("red",2); |
Arrays can also be built using MXML:
<mx:Array id="myArray"/> <!--creates an array with zero elements--> |
One property familiar to all developers is the length property. The length property represents the number of array elements.
private var myArray:Array = new Array(10); |
The length property can be very handy. One popular technique involves using the length property to dictate the number of loops included in a For Loop.
for (var i:uint; i < myArray.length; i++) { |
By utilizing the methods of the Array class, you can perform a number of operations on the Array elements or the Array itself. Popular Array methods include:
* filter
* indexOf
* join
* push
* reverse
* slice
* sort
* toString
However, despite all of an Array's powerful features, the inability to support data binding is a significant shortcoming. Fortunately, Flex provides an ArrayCollection.
ArrayCollection
Per the Flex Language Reference, an ArrayCollection is a "wrapper class that exposes an Array as a collection that can be accessed and manipulated using the methods and properties of the ICollectionView or IList interfaces".Two members of the ArrayCollection class are integral to the ArrayCollection's ability to support data binding - The collectionChange event and addEventListener method. To participate in data binding, an object must be able to:
1. dispatch an event when something changes
2. allow other objects to listen and respond to events
The ArrayCollection satisfies both thanks to the collectionChange event and the addEventListener method and will support data binding when given a compiler directive to do so using the [Bindable] metadata tag.
// creates a bindable ArrayCollection with zero elements |
// creating ArrayCollection with zero elements |
Note: In Flex, ActionScript classes can have optional constructor parameters only. MXML does not enforce required constructor parameters, therefore, if a constructor parameter is required to create a new instance of a class, provide a default value. In the case of an ArrayCollection, the default argument for the source parameter is NULL, hence, an empty ArrayCollection is created when the source parameter is omitted.
Extending the ListCollectionView class that implements the ICollectionView and IList interfaces, an ArrayCollection inherits a handful of properties, methods and events. Therefore, your plain old Array now has a new set of functionality.
New properties include (but not limited to):
* filterFunction
* length
* list
* sort
* source
New methods include (but not limited to):
* addEventListener
* addItem
* addItemAt
* contains
* getItemAt
* getItemIndex
* refresh
* removeAll
* removeItemAt
* setItemAt
Once an Array is wrapped inside an ArrayCollection, the properties and methods of the Array class are no longer accessible unless you access them through the Array object directly. For example, if you really want to use the Array's push method, you could do the following:
myArrayCollection.source.push("new element"); |
myArrayCollection.source.push("new element"); |
myArrayCollection.addItem("new element"); |
Any data manipulation performed on an ArrayCollection will be reflected in the underlying Array and vice-versa, however, it is important to note that manipulating an Array directly will not be reflected inside an ArrayCollection until you perform a refresh of the ArrayCollection using the ArrayCollection's refresh method.
Using an ArrayCollection in place of or in tandem with an existing Array provides Flex developers with a valuable and responsive data storage and retrieval mechanism.
18 comments:
Thanks! That totally answered everything I had been wanting to know for the last hour.
Nice overview
nice overview
Great Thanks
nicely done...
Good!!! Thank you!!!
Its nice - how to access / update individual array elements in arraycollection?
Access ArrayCollection elements with an arraycollection's getItemAt() method and update ArrayCollection elements with an arraycollection's setItemAt() method.
Thorough and clear. Thanks.
Simply Superb!! short and sweet.
The post is really informative.. But if you could explain the significance of Array and Array collections with a sample program, it will be more useful.. Keep up the good work!!!!
This cleared SO much up for me, even after reading all the Adobe documentation. THANK YOU, THANK YOU, THANK YOU!
Great post. Took me awhile to put it together, so posting this in the hopes that it might save some folks time. I needed to create a form whose multiple submissions were stored in an arrayCollection for display within a datagrid (adding mutliple contact addresses). The trick is to place the form values into an object, then add that object to the arrayCollection:
private function funcSaveContact():void {
var tempObj:Object = new Object();
tempObj.contactName = inputContactName.text;
tempObj.contactPhone = inputContactPhone.text;
arrayContacts.addItem(tempObj);
}
The datagrid uses the arrayContacts as its data provider with column values contactName and contactPhone.
source property can be used as the source for data binding. When this property is modified, it dispatches the listChanged event. as given in documentation
Great explanations!!!
Excellent
Superb!!
Hey Thanks a lot :) Now no need to go anywhere to find information...
You saved my brain from getting confused and also my time and energy.
Post a Comment