다음은 ArrayCollection의 속성값으로 인덱스를 구하는 예제입니다. getItemIndex() 메소드는 객체 자체를 참조하기 때문에, 속성값으로 Index를 구할 수 없습니다.

 

 

 

<?xml version="1.0" encoding="utf-8"?>

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

                     xmlns:s="library://ns.adobe.com/flex/spark"

                     xmlns:mx="library://ns.adobe.com/flex/mx">

      <fx:Script>

            <![CDATA[

                  import mx.collections.ArrayCollection;

                  import mx.controls.Alert;

                  import mx.events.FlexEvent;

                 

                  [Bindable]public var dataList:ArrayCollection = new ArrayCollection([{name:"Kevin"}, {name:"Tom"}]);

                 

                  public function getItemIndexByProperty(array:ArrayCollection, property:String, value:String):Number

                  {

                        for (var i:Number = 0; i < array.length; i++)

                        {

                             var obj:Object = Object(array[i])

                             if (obj[property] == value)

                                   return i;

                        }

                        return -1;

                  }

                 

                  protected function creationCompleteHandler():void

                  {

                        var selectedIndex:int = getItemIndexByProperty(dataList, "name", myList.selectedItem.name);

                        Alert.show(selectedIndex.toString() + " : " + dataList.getItemAt(selectedIndex).name);

                  }

            ]]>

      </fx:Script>

      <s:List dataProvider="{dataList}" itemRenderer="IR" click="creationCompleteHandler();" id="myList"/>

</s:Application>

*source : http://stackoverflow.com/questions/8585300/flex-arraycollection-getitemindex-always-return-1

*source code : getItemIndexExample.fxp

저작자 표시 비영리
신고
YOUR COMMENT IS THE CRITICAL SUCCESS FACTOR FOR THE QUALITY OF BLOG POST