| Flex 2 開発ガイド > Flex アプリケーションのユーザーインターフェイスの作成 > データプロバイダおよびコレクションの使用 > IList インターフェイスのメソッドとプロパティの使用 | |||
IList インターフェイスは、リニアデータにインデックスを使用してアクセスするための単純な手段を提供します。このインターフェイスは、基になるデータプロバイダの直接的な表現を提供します。したがって、コレクションを変更するすべての処理が、同じようにデータプロバイダを変更します。つまり、コレクションの 3 番目のアイテムを挿入した場合、基になるデータソースでもそれは 3 番目のアイテムにもなります。
|
メモ |
|
ICollectionView インターフェイスを使用してコレクションをソートまたはフィルタする場合は、IList インターフェイスを使用してデータを操作しないでください。そうすると、結果が不定になります。 |
このインターフェイスには、次のことを実行できるプロパティとメソッドが含まれています。
IList インターフェイスのメソッドとプロパティは、次のクラスまたはプロパティで直接使用できます。
dataProvider プロパティ次のサンプルコードでは、ArrayCollection オブジェクトと、その IList インターフェイスメソッドの実装を使用して、ComboBox コントロールにエレメントの配列を表示します。IList インターフェイスのメソッドを使用して、複数のフィールドを持つオブジェクトの ArrayCollection を管理する例については、例 : DataGrid コントロール内のデータの変更を参照してください。
次の例では、初期状態の Array データソースは次のエレメントで構成されています。
"AZ", "MA", "MZ", "MN", "MO", "MS"
Button コントロールをクリックすると、IList インターフェイスの length プロパティといくつかのメソッドを使用して次のことが行われます。
MA, ME, MI, MN, MO, MS, MT
このコードには、データプロバイダに対する変更を説明するコメントが含まれています。
<?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