10 Replies Latest reply on Apr 21, 2010 9:26 AM by onuratamer

    TimeEvent and ItemRenderer together not working ?

    onuratamer

      Hello

      I am in trouble with my itemRenderer and I dont know if I am doing something wrong ,

      A very easy bunch of code

       

       

       

      
       public class BlinkRenderer extends Text 
          {
              private var blinkTimer:Timer;
               public class BlinkRenderer extends Text 
          {
              private var blinkTimer:Timer;        
              private var _data:Object;
      
                
                override public function set data(value:Object):void{
                     _data = value;
                     text = _data.value;
                }
      
              public function BlinkRenderer():void
              {                    
                  this.blinkTimer = new Timer( 1000 , 0 );
                  this.blinkTimer.addEventListener( "timer" , toggleText );
                  this.blinkTimer.start();
              }
      
              public function toggleText( event:TimerEvent ):void
              {
                  if( this.visible ){            
                      this.visible = false;                
                  }else{            
                      this.visible = true;                
                  }        
              }
      
          }
           
      }
      
      
      
      
      
      
      

       

      And here is my ItemRenderer

       

       

      <mx:DataGrid id="datagrid" rowCount="1" >

      <mx:columns>

      <mx:DataGridColumn id="my" itemRenderer="BlinkRenderer" dataField="value" />

      </mx:columns>

      </mx:DataGrid>

       

       

      The weird part is after setting dataprovider of datagrid  , I cannot clear that value from ItemRenderer NO WAY

       

       

      if(i%2==0)

           datagrid.dataProvider = d;

      else

           datagrid.itemRendererdataProvider = null;

        

      After datagrid get its first value "d" it cannot change content to  null, Always blink first value,

      Is there anybody to explain what is going ...

      Thank you

        • 1. Re: TimeEvent and ItemRenderer together not working ?
          saisri2k2 Level 4

          do

          make d as bindable

          datagrid.dataProvider = new ArrayCollection();

          datagrid.invalidateList();

          • 2. Re: TimeEvent and ItemRenderer together not working ?
            Flex harUI Adobe Employee

            I didn't understand the part about clearing values, but renderers should not

            manipulate the visible property.  The DataGrid/List uses visible to handle

            recycling of renderers.  Maybe use alpha instead.

            • 3. Re: TimeEvent and ItemRenderer together not working ?
              onuratamer Level 1

              I think I can't explain the problem exactly. Here is My Application part

              <?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" 
                                creationComplete="onComplete()"
                                xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
                   
                   <fx:Script>
                        <![CDATA[
                                            
                             import mx.collections.ArrayCollection; 
                             [Bindable] private var d:ArrayCollection = new ArrayCollection([ 
                             {value: 1, summaryRow:false}                 
                             ]);
                             
                             [Bindable] private var d1:ArrayCollection = new ArrayCollection([ 
                                  {value: 2, summaryRow:false}                 
                             ]);
                             
                             var i:int = 0;
                             public function onComplete():void{
                                  datagrid.dataProvider = d;
                                  var remarkTimer:Timer = new Timer( 4000, 0 );
                                  remarkTimer.addEventListener( "timer" , toggleText );
                                  remarkTimer.start();                                        
                             }
                             
                             public function toggleText( event:TimerEvent ):void{
                                  if(i%2==0){
                                       datagrid.dataProvider = d;
                                       datagrid.validateDisplayList();
                                  }
                                  else{
                                       datagrid.dataProvider= d1;
                                       datagrid.validateDisplayList();
                                  }
                                       
                                  i++;
                             }
                             
                             
                        ]]>
                   </fx:Script>          
                   
                   <mx:DataGrid id="datagrid" rowCount="1" >
                        <mx:columns>
                             <mx:DataGridColumn id="my" itemRenderer="BlinkRenderer" dataField="value" />
                        </mx:columns>                    
                   </mx:DataGrid>
                   
              </s:Application>
              
               
              

              If you run this application with BlinkRenderer, You will see after datagrid .dataprovider changes, Timer in my renderer don't behave correctly ,The one which created first still works, It is ok because I didn't  removeEventListener but how can ı remove it, I think in this code there are two itemrenderer objects which each one  has its own timer and they are running together..

              • 4. Re: TimeEvent and ItemRenderer together not working ?
                Flex harUI Adobe Employee

                If the renderer gets removed from the display list when you set a new

                dataProvider, you should catch the Event.REMOVED and stop and free the

                timer.

                • 5. Re: TimeEvent and ItemRenderer together not working ?
                  onuratamer Level 1

                  It is exactly my problem, Renderer is not removed from datagrid, how can I do it myself ? And another weird result , Please look at my code below

                  It is blinking first element of array correctly but the second element sucks??

                  Is there any reasonable explanation ??

                   

                  package {
                      
                      import mx.controls.Text;
                      import flash.utils.Timer;
                      import flash.events.TimerEvent;
                  
                      public class BlinkRenderer extends Text 
                      {
                          private var blinkTimer:Timer;
                          
                            private var _data:Object;
                            
                            override public function set data(value:Object):void{
                                 text = value.value;
                                 if(blinkTimer != null)blinkTimer.stop();
                                 if(blinkTimer == null){
                                      trace("new timer created");
                                      this.blinkTimer = new Timer( 2000 , 0 );
                                      this.blinkTimer.addEventListener( "timer" , toggleText );               
                                 }
                                 this.blinkTimer.start();          
                            }
                            
                          public function BlinkRenderer():void
                          {             
                                 
                          }
                          
                          public function toggleText( event:TimerEvent ):void
                          {
                                 trace(text);
                              if( this.visible ){            
                                  this.visible = false;                
                              }else{            
                                  this.visible = true;                
                              }        
                          }
                                                 
                      }
                      
                  }
                  

                  • 6. Re: TimeEvent and ItemRenderer together not working ?
                    onuratamer Level 1

                    I found a solution its comlex but working for now..

                     

                     

                    change toogleText with

                    public function toggleText( event:TimerEvent ):void
                            {               
                                   trace("text"+text);
                                   var obj:String = String(ArrayCollection(DataGrid(listData.owner).dataProvider).getItemAt( DataGridListData(listData).rowIndex).value);
                                   if(obj == listData.label ){
                                        trace("localtexttext"+localtext);
                                     if( this.visible ){            
                                         this.visible = false;                
                                     }else{            
                                         this.visible = true;                
                                     }        
                                   }
                            }
                    

                    • 7. Re: TimeEvent and ItemRenderer together not working ?
                      onuratamer Level 1

                      Is there anybody who show how timers works in item renderer(datagrid is also loaded dynamicly ) , I will be very glad to see any example .. Thank you

                      • 8. Re: TimeEvent and ItemRenderer together not working ?
                        onuratamer Level 1

                        It is solved .. I was totally wrong way . This must be done with custom events

                        • 9. Re: TimeEvent and ItemRenderer together not working ?
                          mgrjeton

                          I too am having the same issue, could you provide your custom event solution please?

                          • 10. Re: TimeEvent and ItemRenderer together not working ?
                            onuratamer Level 1

                            It is my solution and there may be more clever solutions...

                             

                            1 ) define your dispatcher  public static var Dispatcher:EventDispatcher = new EventDispatcher();

                            2 ) Define your custom event

                             

                            <fx:Metadata>

                            [Event(name="changeDisplay", type="MonitorTimerEvent")]

                            </fx:Metadata>

                             

                            3) fire your event from main application

                             

                            MonitorEventDispatcher.Dispatcher.dispatchEvent(new MonitorTimerEvent(MonitorTimerEvent.CHANGE_DISPLAY,MonitorTimerEvent.CODESHARE_UPDATE));

                             

                            4) catch it in your datagrid and do what you want

                            MonitorEventDispatcher.Dispatcher.addEventListener(MonitorTimerEvent.CHANGE_DISPLAY, changeArrayValue);

                             

                            Custom events can be helpful,