1 Reply Latest reply on Oct 18, 2012 12:22 AM by megharajdeepak

    addEventListener in ItemRenderer Flex AS3

    Ankit Tanna

      Hi,

       

      I am facing problem in addingEventListeners in each items of the ItemRenderer as the dispatch takes place but the listener to the event cannot call the necessary function. Please guide through the same.

       

      The event class is as follows:

      The trace statement does get printed in the below class:

       

       

      package com.events

      {

      import flash.events.Event;

       

      public class CurrentDownloadComplete extends Event

      {

        public static const CURRENT_DOWNLOAD_COMPLETE:String = "currentDownloadComplete";

       

        public var currentKey:String = "";

        public var mediaType:String = "";

       

        public function CurrentDownloadComplete(type:String, currentKey:String, mediaType:String, bubbles:Boolean=false, cancelable:Boolean=false)

        {

         super(type, bubbles, cancelable);

         this.currentKey = currentKey;

         this.mediaType = mediaType;

        

        trace("Downloaded this media -----> " + currentKey + " " + mediaType);  // This gets printed in console

        }

      }

      }

      ____________________________

       

      The structure of the renderer is as follows:

       

       

      <?xml version="1.0" encoding="utf-8"?>
      <!--
      Renderer for the List in the ListContent pod.
      -->
      <s:ItemRenderer
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx"
      mouseChildren="true" autoDrawBackground="false"
        width="97%" height="200" cacheAsBitmap="true" creationComplete="init()" >
        <!--<s:layout>
          <s:HorizontalLayout gap="12"/>
        </s:layout>-->
        <s:states>
          <s:State name="normal"/>
          <s:State name="over"/>
      <s:State name="down"/>
        </s:states>

      <s:HGroup height="100%" width="100%" top="20">
        <s:Image source="assets/video_icon.png"  right="0" width="20%" height="60%"/>
        <s:VGroup height="100%" width="75%">
         <s:Label text="{data.title}" width="90%" fontSize="20" fontWeight="bold"/>
         <s:Label text="{data.author}" fontSize="15" />
         <s:Label text="{data.pubDate}" fontSize="15"  color="black"/>
         <s:HGroup width="100%">
          <s:CheckBox id="audioCheckBox" visible="true"
             includeInLayout="true"
             enabled="{myDashboardModel.isAudioCheckBoxEnabled}" label="Audio ({getFileSize(data,1)} mbs)"   change="createContentInfoSO(event)"/>
         
          <s:Button id="btnAudio" label="Audio" height="30" width="100" skinClass="views.skins.LoginButtonSkin"
              visible="true"
              includeInLayout="true" click="playMedia(event)"/>
         
          <s:CheckBox id="videoCheckBox"  label="Video ({getFileSize(data,0)} mbs)"
             visible="true"
             includeInLayout="true"
             enabled="{myDashboardModel.isVideoCheckBoxEnabled}" change="createContentInfoSO(event)"  />
          <s:Button id="btnVideo" label="Video"
              visible="true"
              includeInLayout="true" 
              skinClass="views.skins.LoginButtonSkin" height="30" width="100" click="playMedia(event)"/>
         </s:HGroup>
        </s:VGroup>
       
      </s:HGroup>
      <fx:Script>
        <![CDATA[
         import com.events.CurrentDownloadComplete;
         import com.events.MyDownloadComplete;
         import com.events.PlayMediaEvent;
         import com.model.ContentInfo;
         import com.model.views.DashboardModel;
         import com.model.views.DownloadManager;
        
         import flash.net.SharedObject;
        
         import mx.binding.utils.BindingUtils;
         import mx.collections.ArrayCollection;
        
         import spark.components.Application;
         import spark.events.IndexChangeEvent;
        
         [Bindable]
         private var myDashboardModel:DashboardModel = DashboardModel.getInstance();
        
         private var localContentInfo:ContentInfo;
         [Bindable] private var downloadManager:DownloadManager=new DownloadManager();
         [Bindable] private var isAudioDownloaded:Boolean = false;
         [Bindable] private var isVideoDownloaded:Boolean = false;
       

      private function init():void

         {

         

           systemManager.addEventListener(MyDownloadComplete.MY_DOWNLOAD_COMPLETE,downloadCompleteHa ndler,false,0,true);

           this.owner.addEventListener(CurrentDownloadComplete.CURRENT_DOWNLOAD_COMPLETE, currentDownloadCompleteHandler, false, 0, true);

           this.parent.addEventListener(CurrentDownloadComplete.CURRENT_DOWNLOAD_COMPLETE, currentDownloadCompleteHandler, false, 0, true);

           this.parentApplication.addEventListener(CurrentDownloadComplete.CURRENT_DOWNLOAD_COMPLETE , currentDownloadCompleteHandler, false, 0, true);

          

           if(myDashboardModel.myContentInfoSO.data.savedContent != null)

           {

            myDashboardModel.selectedContentArray = myDashboardModel.myContentInfoSO.data.savedContent;

           }

           

           if(data.enclosure.length != 2)

           {

            checkMediaAvailability();

           }

          

         }

       

        


        private function currentDownloadCompleteHandler(event:CurrentDownloadComplete):void
         {
         
          var mediaType:String = event.mediaType;
          var currentKey:String = event.currentKey;
         
          trace(mediaType + " " + currentKey + "**********This is returned from CurrentDownloadCompleteEvent"); // this statement does not get printed

         }


        
      ]>
      </fx:Script>

      </s:ItemRenderer>

       

       

      The Download manager structure includes the onDownloadComplete() function which needs to dispatch the event:

      I need to dispatch the event on the completion of download...

       

      package com.model.views
      {

      import libraries.....

      public class DownloadManager extends EventDispatcher
      {
        
        public var currentDownloadComplete:CurrentDownloadComplete;
       
         public function DownloadManager():void
        {
         urlStream = new URLStream();
         urlStream.addEventListener(ProgressEvent.PROGRESS, onProgress);
         urlStream.addEventListener(Event.COMPLETE, onComplete);
         urlStream.addEventListener(IOErrorEvent.IO_ERROR, onError);
        
         currentDownloadComplete = new CurrentDownloadComplete(CurrentDownloadComplete.CURRENT_DOWNLOAD_COMPLETE, currentKey, _mediaType,true);
        }

       

       
        private function onComplete(event:Event):void
        {
         trace('Download complete');
         bytesLoaded = 0;
         bytesTotal = 0;
         totalBytesLoaded = 0;
        
         if(urlStream.connected)
         {
           writeFile();
         }
         updateStatusAndBytes(true);
         var obj:Object = new Object();
         obj.mediaType = _mediaType;
         obj.category = currentKey;

       

        
         dispatchEvent(currentDownloadComplete);
          
        }

        }

       

      __________________________________

       

       

      Please help.