6 Replies Latest reply on Jun 9, 2009 2:05 PM by Flex harUI

    List itemRenderer problem

    DigitalSpeciaist

      I am testing a very simple itemRenderer for the List control, the problem is that the itemRenderer set data method is being called three times, instead of one. I know this because I am tracing the data value when the set data metod is called. Why is this happening? See below.

       

       

       

      [Bindable]

      private var simpleArray:Array = new Array("one", "two", "three", "four","five");

      [Bindable]

      private var theData:ArrayCollection = new ArrayCollection (simpleArray);

       

       

       

       

      <mx:List itemRenderer="com.myTestClasses.renderers.testDataAmount" dataProvider="{theData}"/>

       

       

       

       

       

       

       

       

       

      package com.myTestClasses.renderers

      {

       

      import mx.containers.HBox;

          import mx.controls.Label;

          import mx.core.IDataRenderer;

       

          public class testDataAmount extends HBox

          {

       

              private var nameLabel:Label;

              private var ageLabel:Label;

              private var appearanceLabel:Label;

       

              private var _data:Object;

       

              public function testDataAmount() {

                  super();

              }

       

              override public function get data():Object {

                  if(_data != null) {

                      return _data;

                  }

                  return null;

              }

       

              override public function set data(value:Object):void {

              super.data = value;

      trace ("value test: " + value);

                  _data = value;

              }

          }

       

      }

       

       

       

       

       

      //  This is the trace statement output

       

       

      value test: one

      value test: one

      value test: two

      value test: three

      value test: four

      value test: five

      value test: one

      value test: two

      value test: three

      value test: four

      value test: five

        • 1. Re: List itemRenderer problem
          Flex harUI Adobe Employee

          Probably measuring items to determine the size.  You cannot count on how often the data setter will be called.

           

          Alex Harui

          Flex SDK Developer

          Adobe Systems Inc.

          Blog: http://blogs.adobe.com/aharui

          • 2. Re: List itemRenderer problem
            JeffryHouser Level 4

            I've seen this happen during the initial setup of a component.  However, I would not expect it to occur once the component is initialized, not as you scroll through the list of items.  I was unclear if you had

             

            Add in an "onDataChange" handler.  IT may provide some more insight.  [no guarantees though]

             

            To figure out why, you'd have to step through the framework source.

            • 3. Re: List itemRenderer problem
              DigitalSpeciaist Level 1

              The reason I did that test is actually because of my real problem:

              The data property in the itemRenderer is not being reset, it is storing the previous value and concantenating the new values each time.

               

              Am I missing a step? Do I have to reset or invalidate the data value in the itemRenderer on each call, for it to stop storing the previously assigned value?

               

              Thanks for any help, Mates.

              • 4. Re: List itemRenderer problem
                Flex harUI Adobe Employee

                What do you mean by concatenating?  In what you see or when you actually inspect the data property?

                 

                Alex Harui

                Flex SDK Developer

                Adobe Systems Inc.

                Blog: http://blogs.adobe.com/aharui

                • 5. Re: List itemRenderer problem
                  DigitalSpeciaist Level 1

                   

                  It seems like I have to purge the ItemRenderers using the following protected method:

                   

                  purgeItemRenderers();

                   

                  That solved half the problem (where the data property was not being reset). So the data property is only being set once. This is good

                   

                  The last bit to resolve is: my entire itemRenderer is being repeated but without the content, since the content has been purged, as per the code above.

                   

                  Now, how do I clear out the itemRenderer so it is only rendered once each time? I am wondering how to use the invalidateDisplayList and if that will solve the problem.

                   

                  Thanks.

                   

                  • 6. Re: List itemRenderer problem
                    Flex harUI Adobe Employee

                    Purging shouldn't be necessary.  Your renderers shouldn't have concatenating behavior unless you are handling some corner case.  The major feature of List is that it virtualizes renderers.  You can have 100,000 rows of data but if only 7 rows are visible on about 7 (not exactly 7) renderers exist and are recycled to view different rows as you scroll.  The memory required for 100,000 renderers is prohibitive.  Because renderers are recycled, the data setter is fired often.  Purging would cause performance problems because new renderers will need to be created and added to the display list.

                     

                    If you don't need virtualization, consider Repeater and VBox.

                     

                    Alex Harui

                    Flex SDK Developer

                    Adobe Systems Inc.

                    Blog: http://blogs.adobe.com/aharui