5 Replies Latest reply on Dec 20, 2007 12:35 PM by k0nG86

    Removing Items From Array Collection

    k0nG86
      Hi. I have an array collection which is made up of items defined in class PlayListEntry. I want to remove all the items that have the value of property select set to false. This is what i have come up with, it works fine, but only removes half the items at a time. I think this is because when you remove and item with removeitemat() it shifts the index of the items. How can i get around this?

      Code

      private function removeitems():void{

      for each (var ple:PlayListEntry in songCollection){
      if (ple["select"] != true){
      songCollection.removeItemAt(songCollection.getItemIndex(ple));

      }
      }
      }
        • 1. Re: Removing Items From Array Collection
          Garyl Woolworth Level 1
          Here's a sample application I wrote that achieves what you're looking for.

          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">

          <mx:Script>
          <![CDATA[

          import mx.collections.ArrayCollection;

          [Bindable] private var medalsAC:ArrayCollection = new ArrayCollection([
          {Country:"USA", Gold:35, Silver:39, Bronze:29, select:true},
          {Country:"China", Gold:32, Silver:17, Bronze:14, select:true},
          {Country:"Russia", Gold:27, Silver:27, Bronze:38, select:true},
          {Country:"USA2", Gold:35, Silver:39, Bronze:29, select:false},
          {Country:"China2", Gold:32, Silver:17, Bronze:14, select:false},
          {Country:"Russia2", Gold:27, Silver:27, Bronze:38, select:false},
          {Country:"USA3", Gold:35, Silver:39, Bronze:29, select:true},
          {Country:"China3", Gold:32, Silver:17, Bronze:14, select:true},
          {Country:"Russia3", Gold:27, Silver:27, Bronze:38, select:true}
          ]);

          private function filterItems():void {
          for(var i:Number = 0; i < medalsAC.length; i++){
          if(medalsAC .select == false){
          // => Remove item
          medalsAC.removeItemAt(i);
          // => Refresh collection so it see's new change.
          medalsAC.refresh();
          // => Start at beginning and keep looking
          i = 0;
          }
          }
          }
          ]]>
          </mx:Script>

          <mx:Button x="10" y="10" label="Remove False Items" click="filterItems()"/>
          <mx:DataGrid left="10" right="10" top="35" bottom="10" dataProvider="{medalsAC}">
          <mx:columns>
          <mx:DataGridColumn headerText="Country" dataField="Country"/>
          <mx:DataGridColumn headerText="Gold" dataField="Gold"/>
          <mx:DataGridColumn headerText="Silver" dataField="Silver"/>
          <mx:DataGridColumn headerText="Bronze" dataField="Bronze"/>
          <mx:DataGridColumn headerText="Select" dataField="select"/>
          </mx:columns>
          </mx:DataGrid>
          </mx:Application>
          • 2. Re: Removing Items From Array Collection
            k0nG86 Level 1
            I was so close to this earlier. I just wasn't reseting i to 0. Only problem i've got now is that i can't seem to reference select. The array collection is built in an external class... any suggestions? It wont work as songCollections.song as you suggested.

            Thanks so much for your help.
            • 3. Re: Removing Items From Array Collection
              Garyl Woolworth Level 1
              Is the array collection songCollection within the class set as public? You should be able to do PlayListEntry.songCollection .song if it's public. Or you could have a method within PlayListEntry that returns the songCollection so you could have a collection outside of the class that has the same data. Something like this.

              var songs:ArrayCollection = PlayListEntry.getSongCollection();

              and inside of PlayListEntry you would have

              public function getSongCollection():ArrayCollection {
              return songCollection;
              }

              Without seeing more of your code of how your using it I'm not sure how I can better help you. Hope this helps though.
              • 4. Removing Items From Array Collection
                Garyl Woolworth Level 1
                For whatever reason the post didn't copy my code completely. Within the function of filterItems() it doesn't have the after medalsAC the line should read if(medalsAC[ i ].select == false){
                • 5. Re: Removing Items From Array Collection
                  k0nG86 Level 1
                  Perfect! that works a treat. Much thanks!