4 Replies Latest reply on Jan 20, 2012 8:31 AM by tomexx

    Can I load an array into a dropdown list dynamically?

    tomexx

      Hi,

      I'm VERY new to FLEX basically found it yesterday and started playing with it.

      Looking for a tool to build multi-platform apps for tablets (android/ipad/Blackberry playbook).

       

      I was able to quickly put some controls on the screen and fire events in the FlashBuilder simulator.

       

      The thing I can't find is: I need to populate a dropdown list with 100 consecutive numbers dynamically with the press of a button?

       

      I don't want to hard wire the numbers in the <s: list> tag as the numbers will be different depending on what user picks.

       

      I really wanted to fill an array and connect it to the listbox much like in ActionScript.

       

      Please help,

      Thanks,

      Tomexx

        • 1. Re: Can I load an array into a dropdown list dynamically?
          jfb00 Level 3

          You lucky day

          Best,

           

          <?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:HorizontalLayout verticalAlign="middle" horizontalAlign="center" />

              </s:layout>

              <fx:Declarations>

                  <!-- Place non-visual elements (e.g., services, value objects) here -->

                  <mx:ArrayCollection id="ac">

                      <fx:String>Alabama</fx:String>

                      <fx:String>Alaska</fx:String>

                      <fx:String>Arizona</fx:String>

                      <fx:String>Arkansas</fx:String>

                      <fx:String>California</fx:String>

                  </mx:ArrayCollection>

              </fx:Declarations>

              <fx:Script>

                  <![CDATA[

                      private function getData():void{

                          ddl.dataProvider = ac;

                      }

                  ]]>

              </fx:Script>

           

              <s:DropDownList id="ddl" width="140" />

              <s:Button label="Push Data" click="getData()" />

          </s:Application>

          • 2. Re: Can I load an array into a dropdown list dynamically?
            tomexx Level 1

            Thanks jfb00 however you're still hardcoding the strings in the array.

            What I need is to re-load the array dynamically, clear the items in the listbox then update the list from the array.

             

            Is something like this possible?

             

            STEP1:   load my list with 10 numbers from 1 to 10

               for (var j:int=1; j<=10; j++)

               {

                arrNumbers.addItem({label: j.toString()});

               }

               // load the array to list

               list = arrNumbers...

             

            STEP2:  clear my list

               list.clear...

             

            STEP3: re-load my list with DIFFERENT 10 numbers from 120 to 130

               for (var j:int=120; j<=130; j++)

               {

                arrNumbers.addItem({label: j.toString()});

               }

               // load the array to list

               list = arrNumbers...

             

            Does it make sense?


            Thanks,

            Tomexx

            • 3. Re: Can I load an array into a dropdown list dynamically?
              jfb00 Level 3

              Before you load the list again use

              arrNumbers.removeAll(); them you step 1.

              Best,

              • 4. Re: Can I load an array into a dropdown list dynamically?
                tomexx Level 1

                BTW, I don't have a flashbuilder to try any of this right now, will have to try it later.

                 

                ok so that's how you clear the array. But how do you populate the array in the first place then update the list.

                 

                    <fx:Script>

                        <![CDATA[

                            private function populateArray():void

                            {

                                 for (var j:int=1; j<=10; j++) 

                                 {

                                      arrNumbers.addItem({label: j.toString()});

                                 }

                            }

                 

                            private function getData():void{

                                populateArray();

                                ddl.dataProvider = arrNumbers;

                            }

                        ]]>

                    </fx:Script>

                 

                 

                But how are the <s: ...> tags come into all this? I need to clear the dropdown list before repopulating from the array...

                Sorry I'm totally lost, can you show me the complete 3 steps please with all the declarations in both CDATA and <s:...> thingy?

                Thanks,

                Tomexx