3 Replies Latest reply on Dec 1, 2010 9:22 AM by flex2008

    How to add data to a Spark list control dynamically

    linrsvp

      Hi, I have a spark list control(id="Cclist") in one of my custom  components() and a text input control. When a value is entered into the  text input, I want to dynamically add the same into the list control. I  tried doing the following :

       

      protected function Cc_selectHandler(event:CustomEvent):void

      {

       var cctext:DisplayObject = event.data as DisplayObject
      CcList.enabled = true;
      CcListButton.enabled = true;

      CcList.addChild(cctext);

      }

       

       

      But I get an error saying "addChild() is not available in this class.  Instead, use addElement() or modify the skin". So I tried using  addElement, but apparently that isnt available at all. Any idea what im  doing wrong ?

        • 1. Re: How to add data to a Spark list control dynamically
          flex2008 Level 3

          Add the text entered in the textinput to the dataProvider of the list in the appropriate format.

           

          A simple sample: 

           

          <?xml version="1.0" encoding="utf-8"?>
          <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                         xmlns:s="library://ns.adobe.com/flex/spark" 
                         xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
              <s:layout>
                  <s:VerticalLayout/>
              </s:layout>
          
              <fx:Script>
                  <![CDATA[
                      import mx.collections.ArrayCollection;
                      
                      [Bindable]
                      private var dataProviderAC:ArrayCollection = new ArrayCollection([{name:'Test'},{name:'John'},{name:'jack'}]);
                      
                      protected function button1_clickHandler(event:MouseEvent):void
                      {
                          // TODO Auto-generated method stub
                          var enteredText:String = textInput.text;
                          if(enteredText.length)
                          {
                              dataProviderAC.addItem({name:enteredText});
                          }
                      }
                  ]]>
              </fx:Script>
          
              <fx:Declarations>
                  <!-- Place non-visual elements (e.g., services, value objects) here -->
              </fx:Declarations>
              <s:TextInput id="textInput" width="150"/>
              <s:Button label="Add to list" click="button1_clickHandler(event)"/>
              <s:List width="150" dataProvider="{dataProviderAC}" labelField="name" height="250"/>
          </s:Application>
          
          • 2. Re: How to add data to a Spark list control dynamically
            linrsvp Level 1

            oh I see... thank u so much!! and how do I retrieve all the values from this list ? if I needed just one value I guess I would use list.getChildAt.. but I want all the values... oh i guess I shld just retrive the values from the arraycolection right ?thank u so much once again!

            • 3. Re: How to add data to a Spark list control dynamically
              flex2008 Level 3

              yes.you can use the array collection.