Mar 24 2006

Flex 2 itemRenderer: Bug or bonehead?

Posted by Joe Rinehart at 8:43 AM
3 comments
- Categories: Flex and ColdFusion

I'm starting to work with Flex 2 at night, in between Model-Glue 2.0 and CFUnited presos. I wanted to get the hang of custom item renderers in the list controls last night, so I started fooling around with them.

It went really well for a while: I made a simple list, bound it to a dataprovider, and dropped in an inline renderer that displayed a checkbox next to the label. However, when I check the checkbox and then scroll, wackiness happens: the checkbox may/may not stay checked, others become checked, etc...

You can view this at http://clearsoftware.net/zzztemp/Scratch.swf.

It looks like it has to do with how the control renders things - if I display 5 items, and check the first, then scroll, the sixth is shown as checked as well.

Looking at the code below, can anyone tell me if this is a bug, or if I'm making a boneheaded noob mistake?

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*"   layout="vertical" width="100%" height="100%">
   <mx:Script>
      <![CDATA[
         import mx.collections.ArrayCollection;
         
         public function getItems():ArrayCollection {
            var i:int;
            var items:ArrayCollection = new ArrayCollection();
                        
            for (i=0;i<50;i++) {
               var item:Object = new Object();
               item["label"] = i.toString();   
               items.addItem(item);
            }
            
            return items;
         }
      ]]>
   </mx:Script>

   <mx:List dataProvider="{getItems()}" width="200" height="200">
      <mx:itemRenderer>
         <mx:Component>
            <mx:Canvas>
               <mx:CheckBox id="complete" width="20" x="10" y="4" />
               <mx:Text id="taskname" text="{data.label}" x="27" width="100%" y="4" selectable="false" />         
            </mx:Canvas>
         </mx:Component>
      </mx:itemRenderer>
   </mx:List>
</mx:Application>

Comments

Dirk

Dirk wrote on 03/24/06 10:19 AM

Two things: call the getItems() function only once (e.g. use the creationComplete event of the Application tag), I guess it's refilling your list too often.

Second: clicking on a CheckBox has to modify the dataProvider, otherwise you'll never see the changes (itemRenderers get recycled). Add the following to the CheckBox:

selected=&quot;{data.selected}&quot; click=&quot;data.selected=complete.selected&quot;

Dirk.
Joe Rinehart

Joe Rinehart wrote on 03/24/06 11:25 AM

Thanks Dirk - that made my mistake obvious. I've posted a new entry that updates the code with the fix.
Marco

Marco wrote on 09/29/08 3:42 AM

I'm starting to work with Flex 2 at night, in between Model-Glue 2.0 and CFUnited presos. I wanted to get the hang of custom item renderers in the list controls last night, so I started fooling around with them.
http://www.batteryfast.co.uk/compaq/146630-001.php battery for COMPAQ M300 N400 146630-001 291694-001 laptop battery,
http://www.batteryfast.co.uk/compaq/116314-001.php Battery fits COMPAQ Presario 1200 1600 1800 116314-001 laptop battery,
http://www.batteryfast.co.uk/compaq/pp2162s.php Battery For Compaq Presario 311227-001 PP2162S Laptop laptop battery,
http://www.batteryfast.co.uk/compaq/232060-001.php battery for COMPAQ N150 PP2111X 232060-001 231962-001 laptop battery,
http://www.batteryfast.co.uk/dell/d9200.php Notebook Laptop Battery for Dell D9200 D5318 G5260 laptop battery,
http://www.batteryfast.co.uk/dell/m1210.php 6600mAh Battery fits DELL XPS M1210 NF343 HF674 NEW laptop battery,

http://www.batteryfast.co.uk/dell/312-0341.php 3cell Battery for Dell Latitude X1 T6840 312-0342 Y6457 laptop battery,
http://www.batteryfast.co.uk/dell/xd187.php New Dell Inspiron B130 1300 b120 Battery 312-0416 56whr laptop battery,
http://www.batteryfast.co.uk/gateway/m680.php Gateway M360 M460 M680 8-Cell Notebook Battery 6500949 laptop battery,
http://www.batteryfast.co.uk/hp/hstnn-db17.php New Battery for HP M2000 Series DV1000 DV4000 laptop battery,
http://www.batteryfast.co.uk/hp/f2019b.php Battery For HP F2019 F2019A F2019B 6000 VT6200 XT6200 laptop battery,
http://www.batteryfast.co.uk/hp/hstnn-db02.php Battery For HP HSTNN-IB04 346970-001 HSTNN-DB02 DP399A laptop battery,

http://www.batteryfast.co.uk/hp/n3402.php New F1739A Battery For HP XE XE2 Pavilion N3000 N3490 laptop battery,
http://www.batteryfast.co.uk/hp/zt1000.php New Battery For HP Pavilion ZT1000 F2299A F3172B F3172A laptop battery,
http://www.batteryfast.co.uk/ibm/02k7018.php IBM THINKPAD 600 600A 600D 600E 600X 02K7018 BATTERY laptop battery,
http://www.batteryfast.co.uk/gateway/btp-68b3.php Battery for Gateway Solo M500 M505 Medion MD2900 MD6179 laptop battery,
http://www.batteryfast.co.uk/toshiba/pa3382u-1brs.php Battery Fit Toshiba PA3382U-1BRS PA3384U-1BRS laptop battery,
http://www.batteryfast.co.uk/toshiba/pa3421u-1brs.php Battery for Toshiba M30X M35X M40X PA3395U-1BRS PA3421U laptop battery,

Write your comment



(it will not be displayed)