PDA

View Full Version : Simple itemRenderer for List control (problem)


pointer
06-09-2009, 06:48 PM
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}"/>

//This is my custom itemRenderer
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

mattb
06-13-2009, 04:25 AM
Are you scrolling your list by any chance? The list class in Flex reuses item renderers and resets the data value when it's ready to display a new item.

For example if you have 5 items in your array and your list can only show 3 items at a time, it will initially set renderer1 to "one", renderer2 to "two" and renderer3 to "three". If you scroll down one place, it will then reset the data on the existing renderers so renderer1 = "two", renderer2 = "three", renderer3 = "four" etc.

Because of this it's also possible that if your list gets refreshed for any reason (eg a resize) the list may re-appy data to our renderers which is possibly what you're seeing here.

What shouldn't happen is for the same renderer to be given the same data more than once in succession. The binding system shouldn't re-apply the same data to any setter/getter. For example if renderer1.data = "one", the setter for renderer1 should NOT fire if data is set to "one" again. Flex internally runs the getter to ensure it doesn't set the same data twice in succession.

Also your renderer should handle data changes gracefully, so the real answer to your question should probably be "why does it matter" ;)

HTH a bit!