0
votes

I am working with Flex 4.6 AIR application. I have a tile list and there is an image as an itemRenderer. When i search images, the data is not filtered but is selected the indices(selected items) of the tilelist and i set a custom property in arrayCollection(dataProvider) then updateDisplayList called and i start a timer in this function. Which perform play the inner images in the selected item.

Now the problem is when if i search the item and click the non selected item the timer is not stoped. How can i achieve that.

The code of itemRenderer is below

<?xml version="1.0" encoding="utf-8"?>
<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"
                autoDrawBackground="true" buttonMode="true"
                click="itemrenderer2_clickHandler(event)"
                doubleClick="itemrenderer1_doubleClickHandler(event)" doubleClickEnabled="true"
                rightClick="itemrenderer1_rightClickHandler(event)"
                rollOut="itemrenderer1_rollOutHandler(event)"
                rollOver="itemrenderer1_rollOverHandler(event)" useHandCursor="true">

    <fx:Script>
        <![CDATA[
            import customs.customcomponents.LibraryReDownloadComponent;
            import customs.customcomponents.VideoUploadBox;

            import dbconnection.Connection;

            import globalData.DataModel;

            import mx.collections.ArrayCollection;
            import mx.core.FlexGlobals;
            import mx.core.UIComponent;
            import mx.events.ItemClickEvent;
            import mx.managers.PopUpManager;
            import mx.utils.ObjectProxy;
            import mx.utils.UIDUtil;    

            private var isLoaded:Boolean = false;
            private var timer:Timer;
            private var rollOverFlag:Boolean = false;           
            private var countThumb:int = 0;
            private var arrThumnail:ArrayCollection;
            private var loaderThumb:Loader;     
            private var request:URLRequest;
            private var requestThumb:URLRequest;            
            private var loader:Loader;
            private var sqlStatStatus:SQLStatement;
            private var downloadVideoPath:String = "";
            private var videoUrlStream:URLStream;
            private var videoFileStream:FileStream;
            private var uploadvidbox:VideoUploadBox = new VideoUploadBox();
            private var dtStartVideo:Date;
            private var videoFileName:String = "";
            private var videoFile:File;
            private var IsRollOver:Boolean = false;                     

            [Bindable]
            private var modellocator:DataModel=DataModel.getInstance();

            private var connection:Connection = Connection.getInstance();

            override public function set data(value:Object):void
            {
                if(value != null)
                {
                    super.data = value;

                    if(timer != null)
                    {
                        timer.stop();
                        timer.removeEventListener(TimerEvent.TIMER, showFrame); 
                        timer = null;
                    }

                    loaderThumb = new Loader();                 
                    rollOverFlag = false;                   
                    countThumb = 0; 

                    if(flash.system.Capabilities.os.indexOf("Mac") > -1)
                    {
                        requestThumb = new URLRequest("file://" + data.Videothumbnail);
                    }
                    else
                    {
                        requestThumb = new URLRequest(data.Videothumbnail);
                    }

                    loaderThumb.contentLoaderInfo.addEventListener(Event.COMPLETE,onThumbComplete);                 
                    loaderThumb.load(requestThumb); 

                    if(data.VideoBuyFlag == "yes")
                    {
                        if(data.VideoIsDeleted == "No")
                        {
                            bContain.setStyle("borderColor", "#00FF18");
                            bContain.alpha = 1;
                        }
                        else
                        {
                            bContain.setStyle("borderColor", "#888888");
                            bContain.alpha = 0.3;
                        }                                               
                    }
                    else
                    {
                        if(data.VideoIsDeleted == "No")
                        {
                            bContain.setStyle("borderColor", "#C50000");
                            bContain.alpha = 1;
                        }
                        else
                        {
                            bContain.setStyle("borderColor", "#888888");
                            bContain.alpha = 0.3;
                        }                                           
                    }

                    if(data.VideoStatus == "Active")
                    {
                        imgActive.source = "assets/btn_Active.jpg";
                        bContain.alpha = 1;
                    }
                    else
                    {
                        imgActive.source = "assets/btn_InActive.jpg";
                        bContain.alpha = 0.3;
                    }

                    arrThumnail = new ArrayCollection();

                    if(data.Videothumbdata.thumbimage != null)
                    {
                        if(data.Videothumbdata.thumbimage.source.item.length > 1)
                        {
                            for(var i:int = 0; i < data.Videothumbdata.thumbimage.source.item.length; i++)
                            {
                                arrThumnail.addItem(data.Videothumbdata.thumbimage.source.item[i]);
                            }
                        }
                        else
                        {
                            arrThumnail.addItem(data.Videothumbdata.thumbimage.source.item);                            
                        }
                    }                   
                }
            }           

            override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
            {
                super.updateDisplayList(unscaledWidth, unscaledHeight);             
                /* trace(data.Videoname);
                trace("IsRollOver : " + IsRollOver.toString());
                trace("IsSearching : " + data.IsSearching.toString());
                trace("rollOverFlag : " + rollOverFlag.toString());  */         

                if(data.IsSearching == false)
                {
                    if(IsRollOver == false)
                    {                       
                        if(timer != null)
                        {
                            trace("inner");
                            loaderThumb = new Loader();
                            rollOverFlag = false;                           

                            if(flash.system.Capabilities.os.indexOf("Mac") > -1)
                            {
                                requestThumb = new URLRequest("file://" + data.Videothumbnail);
                            }
                            else
                            {
                                requestThumb = new URLRequest(data.Videothumbnail);
                            }

                            loaderThumb.contentLoaderInfo.addEventListener(Event.COMPLETE,onThumbComplete);                 
                            loaderThumb.load(requestThumb);

                            timer.stop();
                            timer.removeEventListener(TimerEvent.TIMER, showFrame);                         
                            countThumb= 0;  
                            System.gc();
                            System.gc();
                        }                       
                    }
                }
                else
                {                   
                    if(rollOverFlag == false)
                    {
                        rollOverFlag = true;                        
                        trace("Hi");
                        timer = new Timer(1000);
                        timer.addEventListener(TimerEvent.TIMER, showFrame);
                        timer.start();
                    }
                    else
                    {
                        if(timer != null)
                        {
                            trace("SecondInner");
                            loaderThumb = new Loader();
                            rollOverFlag = false;                           

                            if(flash.system.Capabilities.os.indexOf("Mac") > -1)
                            {
                                requestThumb = new URLRequest("file://" + data.Videothumbnail);
                            }
                            else
                            {
                                requestThumb = new URLRequest(data.Videothumbnail);
                            }

                            loaderThumb.contentLoaderInfo.addEventListener(Event.COMPLETE,onThumbComplete);                 
                            loaderThumb.load(requestThumb);

                            timer.stop();
                            timer.removeEventListener(TimerEvent.TIMER, showFrame);                         
                            countThumb= 0;  
                            System.gc();
                            System.gc();
                        }                       
                    }  
                }               
            }                   

            protected function showFrame(event:TimerEvent):void
            {
                var file:File;
                trace("hello");
                if(countThumb < arrThumnail.length)
                {
                    if(flash.system.Capabilities.os.indexOf("Mac") > -1)
                    {
                        file = new File("file://" + arrThumnail[countThumb]);
                    }
                    else
                    {
                        file = new File(arrThumnail[countThumb]);
                    }

                    if(file.exists)
                    {
                        loader = new Loader();

                        if(flash.system.Capabilities.os.indexOf("Mac") > -1)
                        {
                            request=new URLRequest("file://" + arrThumnail[countThumb]);
                        }
                        else
                        {
                            request=new URLRequest(arrThumnail[countThumb]);
                        }

                        loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
                        loader.load(request);

                        if(countThumb == (arrThumnail.length - 1))
                        {                   
                            countThumb = 0;
                        }
                        else
                        {
                            countThumb++;                   
                        }
                    }
                    else
                    {
                        countThumb++;
                    }
                }
                else
                {
                    countThumb = 0;
                }                               
            }

            protected function itemrenderer1_rollOverHandler(event:MouseEvent):void
            {           
                if(data.IsSearching == false)
                {
                    rollOverFlag = true;
                    IsRollOver = true;

                    if(arrThumnail != null && arrThumnail.length > 0)
                    {
                        timer = new Timer(1000);
                        timer.addEventListener(TimerEvent.TIMER, showFrame);
                        timer.start();                                  
                    }
                }               
            }

            private function onComplete(event:Event):void 
            {   
                if(rollOverFlag)
                {
                    imgThumb.source = loader;
                }               
            }

            private function onThumbComplete(event:Event):void
            {
                imgThumb.source = loaderThumb;              
            }

            protected function itemrenderer1_rollOutHandler(event:MouseEvent):void
            {           
                if(data.IsSearching == false)
                {
                    rollOverFlag = false;
                    IsRollOver = false;                 
                    imgThumb.source = loaderThumb;

                    if(timer != null)
                    {
                        timer.stop();
                        timer.removeEventListener(TimerEvent.TIMER, showFrame); 
                        timer = null;
                    }

                    countThumb = 0; 
                    System.gc();
                    System.gc();
                }               
            }       

            protected function itemrenderer2_clickHandler(event:MouseEvent):void
            {               
                for(var i:int=0; i < modellocator.libraryvideoac.length; i++)
                {                                       
                    modellocator.libraryvideoac[i].IsSearching = false;             
                }               

                parentDocument.parentDocument.txt_search.text = resourceManager.getString('languages','lblSearchText');             
                parentDocument.parentDocument.unCheckSelection();

                if(data.VideoIsDeleted == "Yes")
                {
                    var popupReDownload:LibraryReDownloadComponent = new LibraryReDownloadComponent();
                    popupReDownload = PopUpManager.createPopUp(UIComponent(this.parentApplication), LibraryReDownloadComponent, true) as LibraryReDownloadComponent;
                    popupReDownload.addEventListener("downloadMovie", redownloadMovie);
                    PopUpManager.centerPopUp(popupReDownload);
                }               
            }   

        ]]>
    </fx:Script>

    <fx:Declarations>
        <mx:NumberFormatter id="numFormat" precision="2"/>
    </fx:Declarations>

    <s:BorderContainer id="bContain" left="2" top="2" width="92" height="67" backgroundAlpha="1"
                       backgroundColor="#000000" borderColor="#030304" borderWeight="3">            
        <s:Image id="imgThumb" width="86" height="61" fillMode="scale" scaleMode="stretch"/>
        <s:Image id="imgActive" right="0" bottom="0" width="15" height="15" buttonMode="true"
                 useHandCursor="true"/>     
    </s:BorderContainer>    
</s:ItemRenderer>
1
The problem is not entirely clear. Could you post some relevant code snippets? - jpop
Hi, jpop please see above the code of item renderer. - BikrantSingh

1 Answers

0
votes

You have timer instantiation at different places in the code. I can't be sure of the actual program flow, but maybe you instantiate the timer two times? In that case program would overwrite the timer variable, and lose reference to the first timer. So, when you call timer.stop() you actually only stop one timer, while the other keeps running in the background.

You didn't set weak reference attribute here:

timer.addEventListener(TimerEvent.TIMER, showFrame);

so the timer will not be garbage collected if you lose its reference (even if you call System.gc() twice :-)). Check that situation out, and try adding a listener with:

timer.addEventListener(TimerEvent.TIMER, showFrame, false, 0, true);