3 Replies Latest reply on Apr 27, 2009 1:47 PM by ntsiii

    Removing a collection of items from a ArrayCollection

    firdosh Level 1

      Hey,

                  I am trying to remove a collection of selected indices from a ArrayCollection object.

       

      orginalList = new ArrayCollection([1,2,3,4,5,6,7,8,9,5,3,5,7,4,3,4,6,7,5,3])
      var removeItemsCollection:Array =[ 0, 3 , 4, 5 , 11];
      var size:int = removeItemsCollection.length;
      
      for(var i:int=0; i < size; i++)
      {          
        orginalList.removeItemAt(_orginalList .getItemIndex(removeItemsCollection[i]));
      }
      
      

       

      But I keep getting a

       

      RangeError: Index '-1' specified is out of bounds.

      at mx.collections::ListCollectionView/removeItemAt()[C:\autobuild\3.2.0\frameworks\projects\ framework\src\mx\collections\ListCollectionView.as:592]

       

      Thanks,

      Firdosh

        • 1. Re: Removing a collection of items from a ArrayCollection
          Michael Borbor Level 4

          The problem is that I don't see an item 0 in your AC but there's one 

          in your array so the first time it tries to delete the item it returns 

          a -1 the search because there isn't an item 0 in your AC you'll 

          probably add some logic to only try to delete an item from the AC if 

          it's found

           

          Sincerely,

           

          Michael

           

          El 27/04/2009, a las 10:04, firdosh <forums@adobe.com> escribió:

           

          >

          Hey,

                      I am trying to remove a collection of selected indices 

          from a ArrayCollection object.

          >

                     orginalList = new 

          ArrayCollection()

                     var removeItemsCollection:Array =[ 0, 3 , 4, 5 , 11];

                      var size:int = removeItemsCollection.length;

          >

                      for(var i:int=0; i < size; i++)

                      {

          >

                           

          orginalList.

          removeItemAt(_orginalList .getItemIndex(removeItemsCollection+));

                      }

          >

          >

          But I keep getting a

          >

          RangeError: Index '-1' specified is out of bounds.

          at mx.collections::ListCollectionView/removeItemAt()[C:\autobuild

          \3.2.0\frameworks \projects\framework\src\mx\collections

          \ListCollectionView.as:592]

          >

          Thanks,

          Firdosh

          >

          • 2. Re: Removing a collection of items from a ArrayCollection
            Gregory Lafrance Level 6

            This code illustrates checking to see if the item exists first:

             

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
              <mx:Script>
                <![CDATA[
                  import mx.collections.ArrayCollection;
                 
                  [Bindable] private var orginalList:ArrayCollection =
                      new ArrayCollection([1,2,3,4,5,6,7,8,9,5,3,5,7,4,3,4,6,7,5,3])
                 
                  private function init():void {       
                    var removeItemsCollection:Array =[ 0, 3 , 4, 5 , 11];
                    var size:int = removeItemsCollection.length;

                    for(var i:int=0; i < size; i++){
                      if(orginalList.contains(removeItemsCollection[i])){
                        orginalList.removeItemAt(orginalList.getItemIndex(removeItemsCollection[i]));
                      }
                    }
                  }
                ]]>
              </mx:Script>
              <mx:TileList dataProvider="{orginalList}" columnWidth="15" columnCount="10"
                width="100%" height="100%"/>
              <mx:Button label="Remove Items" click="init()"/>
            </mx:Application>

            • 3. Re: Removing a collection of items from a ArrayCollection
              ntsiii Level 3

              One major issue with removing items from an array or collection is that when you remove an item, the indices are recalculated.  If you remove itemAt "zero" , at the next iteration of your loop, the previous item 1 is now at zero.

               

              one solution is to remove the items in reverse order, something like this (from memory)

                      for(var i:int=size-1; i >= 0; i--){
                        if(orginalList.contains(removeItemsCollection[i])){
                          orginalList.removeItemAt(orginalList.getItemIndex(removeItemsCollection[i]));
                        }
                      }

               

               

               

              Tracy Spratt