5 Replies Latest reply on Jan 29, 2010 12:34 PM by NonTakenUserName

    Force refresh/redraw of itemRenderer?

    PeakDigital

      I am using a simple CheckBox itemRenderer in a List control. It is exhibiting some odd behavior that I assume is associated to itemRenderer recycling. When I load a record, the function that selects/deselects items sometimes appears to not work. However, if I manually scroll the List up and down, the proper checkboxes become marked or cleared accordingly.

       

      I have read a few posts and articles on the oddities sometimes experienced with itemRenderers, but so far haven't found a solution for this refresh issue.

       

      Each time a new record is loaded, a function runs on the List's dataProvider that sets all items to selected=false. Then the new record's values are looped through and the proper rows marked selected=true.

       

      I tried calling myCheckList.invalidateDisplayList() after updating the selected values, but that did nothing.

       

      Any insight you can offer would be appreciated.

      Thanks.

      Paul

        • 1. Re: Force refresh/redraw of itemRenderer?
          PeakDigital Level 1

          I haven't had any success in solving this yet. If anyone has any ideas or suggestions I'd appreciate seeing them.

           

          Here is my component, based on example at: http://blogs.adobe.com/aharui/2007/03/thinking_about_item_renderers_1.html

           

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:List xmlns:mx="http://www.adobe.com/2006/mxml">
              <mx:Script>
                  <![CDATA[
                  import mx.collections.ArrayCollection;
                  
                  public function checkSelectedItems(arrcollSelectedItems:ArrayCollection):void
                      {
                      trace("Entering CheckList.checkSelectedItems");
                      
                      for(var i:int=0; i<arrcollSelectedItems.length; i++)
                          {
                          for(var n:int=0; n<this.dataProvider.length; n++)
                              {
                              if(arrcollSelectedItems[i].label==this.dataProvider[n].label)
                                  {
                                  this.dataProvider[n].selected=true;
                                  break;
                                  }    
                              }
                          }
                      }
          
                  public function clearAllSelections():void
                      {
                      for(var n:int=0; n<this.dataProvider.length; n++)
                          {
                          this.dataProvider[n].selected=false;    
                          }
                      }
                  ]]>
              </mx:Script>
              
              <mx:itemRenderer>
                  <mx:Component>
                      <mx:CheckBox selectedField="selected" change="data.selected = selected" />
                  </mx:Component>
              </mx:itemRenderer>
          </mx:List>
          
          • 2. Re: Force refresh/redraw of itemRenderer?
            NonTakenUserName

            I'm not sure I'm understanding when it is that the function doesn't work. I'm using the code you posted with the stateList array from the List of Checkboxes example in the site you linked to and everything is getting checked & unchecked correctly.

             

            Could you provide more information? i.e. dataSet / any other code you may have / compiled file with "View source" enabled / screenshot?

            • 3. Re: Force refresh/redraw of itemRenderer?
              John Hall Level 4

              Yea, it's tough without a bit more data stuff but when you change the data, try myList.invalidateList() on the list-based control and see what happens, rather than invalidateDisplayList

              1 person found this helpful
              • 4. Re: Force refresh/redraw of itemRenderer?
                PeakDigital Level 1

                Thank you both for replying.  I will try the .invalidateList() and see if that changes anything.

                 

                Here is a sample of the data I am using. The server generates XML via PHP and returns that to my app, which reads it into an ArrayCollection that is the dataProvider for my List.

                 

                 

                <data>
                <record>
                 <label>Friends</label>
                 <selected>false</selected>
                 <RelationshipID>1</RelationshipID>
                
                </record>
                <record>
                 <label>Relatives</label>
                 <selected>false</selected>
                 <RelationshipID>24</RelationshipID>
                </record>
                <record>
                 <label>Co-workers</label>
                 <selected>false</selected>
                 <RelationshipID>5</RelationshipID>
                </record>
                
                <record>
                 <label>Clients</label>
                 <selected>false</selected>
                 <RelationshipID>3</RelationshipID>
                </record>
                <record>
                 <label>Vendors</label>
                 <selected>false</selected>
                 <RelationshipID>4</RelationshipID>
                </record>
                </data>
                
                

                 

                To clarify when this intermittent problem arises: It occurs when I load a new record programatically. If I just manually check/uncheck boxes there doesn't seem to be a problem. If I load a record with more boxes checked than the record that was there, then the new boxes may or may not become checked. Vice versa if I load a record with fewer boxes checked - not all of them may disappear.

                • 5. Re: Force refresh/redraw of itemRenderer?
                  NonTakenUserName Level 1

                  Ok, I tried the code with the XML dataset and what happened is that every time that you programmatically add records and refresh the ArrayCollection, everything gets deselected (it never happened to me that any one of them remained checked after loading more), but the values in the dataset were still correctly set as checked or unchecked. I assume this is because the itemRenderer is getting loaded; by default, the CheckBox in it is unchecked and we're not setting a selected value in the renderer.

                   

                  I just edited the component MXML and added a selected property for the CheckBox:

                   

                  <mx:CheckBox selectedField="selected" change="data.selected = selected" selected="{data.selected == 'true'}" />

                   

                  With that new bit, all CheckBoxes are correctly remaining selected or delselected when I add more records.