1 Reply Latest reply on Mar 17, 2008 5:34 AM by JKohn99

    ArrayCollection-- getItemIndex

    SiHoop
      I'm trying to get the index of an item in an ArrayCollection, and am expecting the result to be:
      ItemIndex = 1
      but the trace statement produces:
      ItemIndex = -1.
      What am I doing wrong?

      <?xml version="1.0"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml">
      <mx:Script>
      <![CDATA[
      import mx.collections.*;
      public function myButton():void {
      trace("ItemIndex="+myAC.getItemIndex({label:"MT"}))
      }
      ]]>
      </mx:Script>
      <mx:ArrayCollection id="myAC">
      <mx:Array id="myArray">
      <mx:Object label="MA" data="Boston"/>
      <mx:Object label="MT" data="Helena"/>
      <mx:Object label="ME" data="Augusta"/>
      </mx:Array>
      </mx:ArrayCollection>
      <mx:Button label="Click to test" click="myButton();"/>
      </mx:Application>
        • 1. Re: ArrayCollection-- getItemIndex
          JKohn99
          getItemIndex looks for a the object passed in as a parameter.
          You need to pass in the object that is MT, Helena, not the label.
          so :

          var obj1:Object = new Object();
          obj1.label = "MT";
          obj1.data = "Helena";
          myArrayCol.addItem(obj1);

          var idx:int = myArrayCol.getItemIndex(obj1);

          If you need to search via the label the you will need
          to iterate the arraycollection or use a HashTable.