How can I get the ItemRenderer of a Flex Spark List from its DataProvider Object?

Lee Probert picture Lee Probert · Oct 26, 2010 · Viewed 11.2k times · Source

In Flex I can create an ItemRenderer to represent each item in the Lists DataProvider but how do I access the instance of the ItemRenderer via the DataProviders Object? Something like myList.getItemRenderer(dp.getItemAt(10));

Answer

Maxim Kachurovskiy picture Maxim Kachurovskiy · Oct 27, 2010
public function getItemRenderer(list:List, item:Object):ItemRenderer
{
    var dataGroup:DataGroup = list.dataGroup;
    var n:int = dataGroup.numElements;
    for (var i:int = 0; i < n; i++)
    {
        var renderer:ItemRenderer = dataGroup.getElementAt(i) as ItemRenderer;
        if (renderer && renderer.data == item)
            return renderer;
    }
    return null;
}