4 Replies Latest reply on Dec 14, 2010 1:18 AM by Zombiecook KochtVorWut

    remove specific item from arraylist

    Zombiecook KochtVorWut Level 1

      Hey,
      i want simply remove an item from an arraylist.

       

      private function addNewItem():void {
                object = new ObjectProxy();
                object.id = input.text;
                arrayList.addItem(object);
      }
      

       

      Now how i can remove the item from its specific id ?

      something like this:

       

      arrayList.removeItem(object.id=="blabla");

      or

      for each (obj:Object in ArrayList){
                if(obj.id == "blabla"){
                     ArrayList.removeItem(obj.id);
                }
      }


      I hope u can help me.
      Greetings,
      Zombiecook

        • 1. Re: remove specific item from arraylist
          saisri2k2 Level 4

           if(obj.id == "blabla"){
                         ArrayList.removeItem(obj);
                    }

          this should suffice in the for loop, last block of code. (you want to remove obj rite?)

          • 2. Re: remove specific item from arraylist
            Zombiecook KochtVorWut Level 1

            i dont know really if my loop is the right way.
            But i will add many diffrent Object to the ArrayList with different id's and i want to write a function that deletes an specific Object on its id.
            So for example i have added 4 Object to the ArrayList with different id's (Obj Nr. 1 = "001" , Obj Nr. 2 = "002" and so on..) and now i want to delete the Object with the id "002". So how can i do that ?

            • 3. Re: remove specific item from arraylist
              kevinklin Adobe Employee

              Your second code sample is close. Just remove the obj instead of the id when you find it like this:

               

              for each (obj:Object in list){
                        if(obj.id == "blabla"){
                             list.removeItem(obj);
                        }
              }

              Of course, it would be more efficient to just do:

               

              for (var i:int = 0; i < list.length; i++)
              {
                        if(obj.id == "blabla"){
                             list.removeItemAt(i);
                             break;
                        }
              }

               

              -Kevin

              • 4. Re: remove specific item from arraylist
                Zombiecook KochtVorWut Level 1

                Thank you very much.
                I post my code to show an example how it works:

                 

                <?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.ArrayList;
                            import mx.utils.ObjectProxy;
                            
                            [Bindable] private var array:ArrayList = new ArrayList();
                            
                            private var obj:ObjectProxy;
                            
                            protected function addItem(event:MouseEvent):void        {
                                obj = new ObjectProxy();
                                obj.id    = newItemInput.text;
                                array.addItem(obj);
                            }
                
                            protected function deleteItem(event:MouseEvent):void        {
                                for (var i:int = 0; i < array.length; i++)
                                {
                                    if(obj.id == deleteItemInput.text){
                                        array.removeItemAt(i);
                                        break;
                                    }
                                }
                            }
                
                        ]]>
                    </fx:Script>
                    
                    <mx:VBox verticalCenter="0" horizontalCenter="0">
                        <mx:HBox>
                            <s:TextInput id="newItemInput" />
                            <s:Button label="addItem" click="addItem(event)" />
                        </mx:HBox>
                        <mx:HBox>
                            <s:TextInput id="deleteItemInput" />
                            <s:Button label="deleteItem" click="deleteItem(event)" />
                        </mx:HBox>
                        <s:List id="list" dataProvider="{array}" itemRenderer="Skins.testRenderer" >
                            <s:layout>
                                <s:VerticalLayout gap="0" horizontalAlign="justify" />
                            </s:layout>
                        </s:List>
                    </mx:VBox>
                    
                </s:Application>
                

                 

                Thank you again for helping me out

                Greetings
                Zombiecook