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

    ArrayCollection-- getItemIndex

    SiHoop Level 1
      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">
      import mx.collections.*;
      public function myButton():void {
      <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:Button label="Click to test" click="myButton();"/>
        • 1. Re: ArrayCollection-- getItemIndex
          JKohn99 Level 1
          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";

          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.