topical media & game development

talk show tell print

professional-flex-code-06-CollectionChangeEvent.mx

professional-flex-code-06-CollectionChangeEvent.mx [swf] flex


  <?xml version="1.0" encoding="utf-8"?>
  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
  
          <mx:Script>
                  <![CDATA[
                  
                          import mx.events.CollectionEventKind;
                          import mx.events.CollectionEvent;
                          import mx.collections.SortField;
                          import mx.collections.Sort;
                          import mx.collections.ArrayCollection;
                          
                          private var index:Number = 0;
                          
                          public function collectionChange(event:CollectionEvent):void
                          {
                                  switch (event.kind)
                                  {
                                          case CollectionEventKind.ADD:
                                                  myTextArea.text += "Item Added\n";
                                                  break;
                                          case CollectionEventKind.REPLACE:
                                                  myTextArea.text += "Item Replaced\n";
                                                  break;
                                          case CollectionEventKind.REMOVE:
                                                  myTextArea.text += "Item Removed\n";
                                                  break;
                                  }
                          }
                          
                          public function addItem():void
                          {
                                  myCollection.addItem({label: myTextInput.text, data: index});
                                  index++;
                          }
                          
                          private function updateItem():void
                          {
                                  if (myList.selectedItem != null)
                                          myCollection.setItemAt({label: myTextInput.text, data: index}, myList.selectedIndex);
                          }
                          
                          private function removeItem():void
                          {
                                  if (myList.selectedItem != null)
                                          myCollection.removeItemAt(myList.selectedIndex);
                          }
                          
                  ]]>
          </mx:Script>
          
          <mx:ArrayCollection id="myCollection" collectionChange="collectionChange(event)" />
          
          <mx:TextInput id="myTextInput" />
          <mx:List id="myList" dataProvider="{myCollection}" width="200" height="200" />
          <mx:Button label="ADD" click="addItem()" />
          <mx:Button label="UPDATE" click="updateItem()" />
          <mx:Button label="REMOVE" click="removeItem()" />
          <mx:TextArea id="myTextArea" width="200" height="200" />
          
  </mx:Application>
  


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.