The core concepts are that item renderers need to update their visual UI elements when the associated item changes, and need to update that item on user interaction.
You assign a dataProvider(StockInfo) to the List, which results in item renderers being instantiated. Note, do not use an Array as a dataProvider, use ArrayCollection. When the underlying item changes, the framework calls the renderer's set data method(setter). When any pending changes are done, the framework calls commitProperties. You need to use this mechanism because there is no assurance that there will be an instance of any particular renderer to listen of an event. Renderers are recycled. Google that before you go any further. Tracy _____ From: flexcoders@yahoogroups.com [mailto:flexcod...@yahoogroups.com] On Behalf Of flexrookie Sent: Saturday, February 28, 2009 8:59 PM To: flexcoders@yahoogroups.com Subject: [flexcoders] updating an item renderer hi list, first post... i have an architectural question about datatproviders and itemrenderers that i'll pose with a canonical example. suppose i have an array of StockInfo objects that is populated by a 3rd party library. each StockInfo object dispatches UPDATE events when a stock price changes. i want to render the prices in HorizontalList with a custom renderer. here's the general application shell: ========================= <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe. <http://www.adobe.com/2006/mxml> com/2006/mxml" layout="absolute" applicationComplete="applicationCompleteListener(event)"> <mx:Script> <![CDATA[ import mx.controls.HorizontalList; import mx.events.FlexEvent; import mx.core.ClassFactory; protected var list:HorizontalList; protected var stocks:Array; protected function applicationCompleteListener (e:FlexEvent):void { // Stock data (hardcoded for the example) stocks = [new StockInfo()]; // UI list = new HorizontalList(); list.width = 400; list.height = 100; list.itemRenderer = new ClassFactory(StockInfoRenderer); addChild(list); } ]]> </mx:Script> </mx:Application> ========================= i need to wire the stocks array to the list. of course, given that i already have an array of StockInfo objects, it's tempting to make an ArrayCollection wrapper that gives direct access to the StockInfo objects. e.g., provider = new ArrayCollection(); provider.addItem({stockInfo:someStockInfoObject}); then the StockInfoRenderer could register for UPDATE events and redraw when the event occurs. but i'm hesitant about that approach. if i went that route, 1) within the StockInfoRenderer, where would i register for the UPDATE event? in commitProperties()? 2) within the StockInfoRenderer, where would i *remove* the UPDATE listener? 3) given that renderers are reused, are there issues with synching, or with stranded listeners causing memory build-up? more generally, what's the right way to wire my list to the stocks array? it feels like i might be forced to handle UPDATE for each StockInfo object, and write a 'price' variable into a data provider. e.g., provider.getItemAt(i).price = value; provider.itemUpdated(provider.getItemAt(i), "price"); seems a shame, given that i already have an array of StockInfo objects. the above two lines presuppose that i'm going to hunt for the item index every time a stock price changes. : ( another general question, do i even need the itemUpdate() call, or is that part automated? thanks for any advice! flexrookie