| Flex 2 Developer's Guide >
Building User Interfaces for Flex Applications > Using Data Providers and Collections > Using IList interface methods and properties |
|||
The IList interface provides simple methods for indexed access to linear data. The interface provides a direct representation of the underlying data provider. Thus, any operation that changes the collection changes the data provider in a similar manner: if you insert a item as the third item in the collection, it also is the third item in the underlying data source.
|
NOTE |
|
If you use the ICollectionView interface to sort or filter a collection, do not use the IList interface to manipulate the data, as the results are indeterminate. |
The interface includes properties and methods that let you do the following:
You can use the IList interface methods and properties directly on any of the following classes or properties:
dataProvider property of standard Flex controls.The following sample code uses an ArrayCollection object and its implementation of the IList interface methods to display an array of elements in a ComboBox control. For an example that uses IList interface methods to manage an ArrayCollection of objects with multiple fields, see Example: Modifying data in DataGrid control.
In the following example the Array data source initially consists of the following elements:
"AZ", "MA", "MZ", "MN", "MO", "MS"
When you click the Button control, the application uses the length property and several methods of the IList interface to do the following:
MA, ME, MI, MN, MO, MS, MT
The code includes comments that describe the changes to the data provider.
<?xml version="1.0"?>
<!-- dpcontrols\UseIList.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
initialize="initData()">
<mx:Script>
<![CDATA[
import mx.collections.*;
// The data provider is an Array of Strings
public var myArray:Array = ["AZ", "MA", "MZ", "MN", "MO", "MS"];
// Declare an ArrayCollection that represents the Array.
[Bindable]
public var myAC:ArrayCollection;
//Initialize the ArrayCollection.
public function initData():void {
myAC = new ArrayCollection(myArray);
}
// The function to change the collection, and therefore
// the Array.
public function changeCollection():void {
// Get the original collection length.
var oldLength:int=myAC.length;
// Remove the invalid first item, AZ.
var removedItem:String=String(myAC.removeItemAt(0));
// Add ME as the second item. (ILists used 0-based indexing.)
myAC.addItemAt("ME", 1);
// Add MT at the end of the Array and collection.
myAC.addItem("MT");
// Change the third item from MZ to MI.
myAC.setItemAt("MI", 2);
// Get the updated collection length.
var newLength:int=myAC.length;
// Get the index of the item with the value ME.
var addedItemIndex:int=myAC.getItemIndex("ME");
// Get the fifth item in the collection.
var index4Item:String=String(myAC.getItemAt(4));
// Display the information in the TextArea control.
ta1.text="Start Length: " + oldLength + ". New Length: " +
newLength;
ta1.text+=".\nRemoved " + removedItem;
ta1.text+=".\nAdded ME at index " + addedItemIndex;
ta1.text+=".\nThe item at index 4 is " + index4Item + ".";
// Show that the base Array has been changed.
ta1.text+="\nThe base Array is: " + myArray.join();
}
]]>
</mx:Script>
<mx:ComboBox id="myCB" rowCount="7" dataProvider="{myAC}"/>
<mx:TextArea id="ta1" height="75" width="300"/>
<mx:Button label="rearrange list" click="changeCollection();"/>
</mx:Application>
Flex 2.01