10 Replies Latest reply on Aug 10, 2010 11:48 AM by craneium

    How do I use an itemrenderer with array from actionscript???

    craneium

      I am wanting to populating a list control with several Checkbox controls, i understand how to use the itemrenderer but all the examples I have found populate the dataprovider array outside of actionscript i want to know how to create my array inside of action script and pass it to the listbox. when i was playing around with this i could get the listbox to populate with checkbox but it would not so the label of the items, i am guessing i was not passing the right information in the array.

        • 1. Re: How do I use an itemrenderer with array from actionscript???
          Flex harUI Adobe Employee

          Var arr:Array = [];

              arr.push();

              ...

              var al:ArrayList = new ArrayList(arr);

              list.dataProvider = al;

          • 2. Re: How do I use an itemrenderer with array from actionscript???
            craneium Level 1

            yeah thats what i thought but the label propority of the check box is not getting populated. the checkboxes come in but without a label on thim.

            • 3. Re: How do I use an itemrenderer with array from actionscript???
              BhaskerChari Level 4

              Hi craneium,

               

              In your itemRenderer file you need to specify the property to bind fromm your dataprovider which is stored in data object for each itemRenderer...

               

              <mx:CheckBox id="chk" label="{data.label}" />

               

               

              Here label is one of the property that you have in your dataprovider for each object...you can replace data.label with the text property you want to be displayed..

               

              I Hope  you got my point...

               

               

              If this post answers your question or helps, please kindly mark it as such.


              Thanks,

              Bhasker Chari

              • 4. Re: How do I use an itemrenderer with array from actionscript???
                craneium Level 1

                i am still a little confused here is what i have at current state, which is not

                working

                 

                <fx:Script>
                        <![CDATA[
                            public var myarray:ArrayList;
                            public var returnedarray:Array = ["a","b","c","d","e","f","g","h"];
                            public var key:String = "label"
                            public function init():void{
                                myarray = new ArrayList;
                                for (var i:int = 0; i<=(returnedarray.length-1); i++){
                                    var myobj:Object = {};
                                    myobj[key] = returnedarray[i].toString();
                                    myarray.addItem(myobj);
                                }
                                FileList.dataProvider = myarray;
                            }
                        ]]>
                    </fx:Script>

                 

                <s:List id="FileList" x="10" y="99" width="207" height="285" color="#000000" contentBackgroundColor="#FFFFFF" alternatingItemColors="[#ffffff,#eadee0]" borderVisible="true">
                                    <s:itemRenderer>
                                        <fx:Component>
                                            <s:CheckBox label="{myarray.label}"/>
                                        </fx:Component>
                                    </s:itemRenderer>
                                </s:List>

                 

                this throughs a unkown item error (myarray)

                • 5. Re: How do I use an itemrenderer with array from actionscript???
                  MarkTaylor46

                  Use data.label not myarray.label.  Data is passed to an item renderer via data object (which in turn gets sourced from your dataprovider - in your case myarray).

                   

                  regards,

                   

                  Mark.

                  • 6. Re: How do I use an itemrenderer with array from actionscript???
                    craneium Level 1

                    I am still getting and error (1120: Access of undefined property; data.)

                     

                    <s:List id="FileList" x="10" y="99" width="207" height="285" color="#000000" contentBackgroundColor="#FFFFFF" alternatingItemColors="[#ffffff,#eadee0]" borderVisible="true">
                                        <s:itemRenderer>
                                            <fx:Component>
                                                <s:CheckBox label="{data.label}"/>   
                                            </fx:Component>
                                        </s:itemRenderer>

                    • 7. Re: How do I use an itemrenderer with array from actionscript???
                      MarkTaylor46 Level 2

                      your s: list has no dataProvider  you need to add one. I think that's why.

                       

                      Regards, Mark.

                       

                       

                      oops - just noticed ur doing it in actionscript instead of mxml. i presume your init function is invoked on creationcomplete event? try adding a breakpoint in the item renderer and see what it being passed in.

                      • 8. Re: How do I use an itemrenderer with array from actionscript???
                        craneium Level 1

                        man this cannot be this hard, still error 1120.

                         

                        <fx:Declarations>
                                <!-- Place non-visual elements (e.g., services, value objects) here -->
                                <s:ArrayList id="myarray">
                                    <fx:Object label="File Items Here"/>
                                </s:ArrayList>

                        </fx:Declarations>

                         

                        <s:List id="FileList" dataProvider="{myarray}" x="10" y="99" width="207" height="285" >
                             <s:itemRenderer>
                                  <fx:Component>
                                       <s:CheckBox label="{data.label}"/>   
                                  </fx:Component>
                             </s:itemRenderer>
                        </s:List>

                        • 9. Re: How do I use an itemrenderer with array from actionscript???
                          MarkTaylor46 Level 2

                          change your code as shown below and it works. Use an external renderer instead of an inline one.

                           

                          <?xml version="1.0" encoding="utf-8"?>
                          <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
                                                 xmlns:s="library://ns.adobe.com/flex/spark"
                                                 xmlns:mx="library://ns.adobe.com/flex/mx"
                                                 creationComplete="init()">
                              <fx:Declarations>
                                  <!-- Place non-visual elements (e.g., services, value objects) here -->
                              </fx:Declarations>
                             
                              <fx:Script>
                                  <![CDATA[
                                      import mx.collections.ArrayList;
                                      public var myarray:ArrayList;
                                      public var returnedarray:Array = ["a","b","c","d","e","f","g","h"];
                                      public var key:String = "label"
                                      public function init():void{
                                          myarray = new ArrayList();
                                          for (var i:int = 0; i<=(returnedarray.length-1); i++){
                                              var myobj:Object = {};
                                              myobj[key] = returnedarray[i].toString();
                                              myarray.addItem(myobj);
                                          }
                                          FileList.dataProvider = myarray;
                                      }
                                  ]]>
                              </fx:Script>
                             
                             
                             
                              <s:List id="FileList" x="10" y="99" width="207" height="285" color="#000000" contentBackgroundColor="#FFFFFF" alternatingItemColors="[#ffffff,#eadee0]" borderVisible="true" itemRenderer="myItemRenderer">
                                 
                              </s:List>
                          </s:WindowedApplication>

                           

                           

                           

                           

                           

                          then create "myItemRenderer" in the same folder....

                           

                           

                          <?xml version="1.0" encoding="utf-8"?>
                          <s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
                                          xmlns:s="library://ns.adobe.com/flex/spark"
                                          xmlns:mx="library://ns.adobe.com/flex/mx"
                                          autoDrawBackground="true">
                              <s:CheckBox label="{data.label}"/>
                             
                          </s:ItemRenderer>

                           

                           

                          I hope this is what you were expecting.

                           

                           

                          Regards,

                           

                          Mark.