1 Reply Latest reply on Dec 28, 2010 7:44 AM by Gregory Lafrance

    what's wrong with my code?

    Jun yuan Level 1

      The mxml  code is as follows:

       

       

      <?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" minWidth="955" minHeight="600" xmlns:deviceservice="services.deviceservice.*">

      <fx:Script>

      <![CDATA[

      import mx.collections.ArrayCollection;

      import mx.controls.Alert;

      import mx.events.DragEvent;

      import mx.events.FlexEvent;

       

      import spark.events.IndexChangeEvent;

       

      //[Bindable]public var totalPrice:int =0;

       

       

      private function initApp():void {

      firstList.dataProvider = new ArrayCollection([

      {label:"First", data:"1",value:"first"},

      {label:"Second", data:"2",value:"second"},

      {label:"Third", data:"3",value:"third"},

      {label:"Fourth", data:"4",value:"fourth"}

      ]);

      secondList.dataProvider = new ArrayCollection([]);

       

      }

       

       

       

       

       

       

      protected function initbut_clickHandler(event:MouseEvent):void

      {

      initApp();

      }

       

       

      public function popupbut_clickHandler(event:MouseEvent):void

      { var totalPrice:Number=0;

      totalPrice+=int( secondList.selectedItem.data);

       

      Alert.show(" The total price is:"+totalPrice );

      }

       

       

       

       

       

       

       

       

       

       

      protected function secondList_dragDropHandler(event:DragEvent):void

      {

       

       

       

       

      var dragObj:Array=

      event.dragSource.dataForFormat("items") as Array;

       

       

      // Get the Artist for all dragged albums.

      var artistList:String='';

      var total:Number=0;

      for (var i:Number = 0; i < dragObj.length; i++) {

      artistList+='Artist: ' + dragObj[i].data + '\n';

      total+=dragObj[i].data;      

      }

      Alert.show(artistList+total);

      }

       

       

       

       

       

       

      protected function firstList_creationCompleteHandler(event:FlexEvent):void

      {

      initApp();

      }

       

      ]]>

      </fx:Script>

       

      <s:List x="73" y="106" width="125" id="firstList"  dragEnabled="true"

      dragMoveEnabled="true" dropEnabled="true"  allowMultipleSelection="true" creationComplete="firstList_creationCompleteHandler(event)" ></s:List>

      <s:List x="409" y="106" width="162" id="secondList"

      dropEnabled="true" dragEnabled="true"

      dragMoveEnabled="true"

       

      dragDrop="secondList_dragDropHandler(event)" ></s:List>

      <s:Button x="187" y="245" label="Init app" id="initbut" click="initbut_clickHandler(event)"/>

      <mx:PopUpButton x="359" y="266" label="PopUpButton" id="popupbut"  click="popupbut_clickHandler(event)"/>

      <s:Button x="165" y="303" label="Button"/>

      <s:Label x="117" y="68" text="Available"/>

      <s:Label x="471" y="72" text="Current"/>

       

      </s:Application>

        • 1. Re: what's wrong with my code?
          Gregory Lafrance Level 6

          I think your dragdrop handler was getting called during the drag from list 1 to list 2, which is not what your code is set up to do.

           

          Try this:

           

          protected function secondList_dragDropHandler(event:DragEvent):void {
            var dragObj:Array = event.dragSource.dataForFormat("items") as Array; 
            // Get the Artist for all dragged albums.
            var artistList:String='';
            var total:Number=0;
            if(dragObj != null){   //  check for null here
              for (var i:Number = 0; i < dragObj.length; i++) { 
                artistList+='Artist: ' + dragObj[i].data + '\n';
                total+=dragObj[i].data;       
              }
              Alert.show(artistList+total); 
            }
          }
          


          If this post answers your question or helps, please mark it as such. Thanks!

          http://www.chikaradev.com
          Adobe Flex Development and Support Services