4 Replies Latest reply on Aug 22, 2011 9:38 AM by phaseblue

    How Do You Populate A Spark List Control With An Array?

    phaseblue

      Hello, all,

       

      Sorry to come accross so frustrated, but how in the name of God do you populate a Spark list control with the data in an array?  You used to be able to do this with the mx:List control, but the guys developing Flex just had to make things more difficult than they need to be!  I am more of a code purist and prefer doing things the way they have been done for decades, but apparently nothing can ever stay simple!

       

      I simply want to populate a list control with an array and this shouldn't be rocket science!  I found out that I must use a "collection" element, so I decided that an arrayCollection would be best.  However, after searching Adobe's documentation about arrayCollections, I am lost in a black hole of data binding, extra lines of code just to add a new element, the need to sort it, etc...!

       

      Here is my code:

      var pendingArray:ArrayCollection = new ArrayCollection();
      
      for ( var i:int = 0 ; i < queue.length ; i++ )
      {
           var item:UserQueueItem = queue[i] as UserQueueItem ;
           if ( item.status == UserQueueItem.STATUS_PENDING )
           {
           pendingArray.addItem({label:item.descriptor.displayName,descriptor:item.descriptor});
           }
      }
      

       

      Here is the relevant MXML:

      <s:VGroup>
           <s:List id="knockingList" width="110" height="100"/>               
      </s:VGroup>
      


      I'm not getting any errors, but the list is not populating.

       

      I have seen several examples where the arrayCollection is declared and populated in MXML:

              <mx:ArrayCollection id="myAC">
                  <!-- Use an fx:Array tag to associate an id with the array. --> 
                  <fx:Array id="myArray">
                      <fx:Object label="MI" data="Lansing"/>
                      <fx:Object label="MO" data="Jefferson City"/>
                      <fx:Object label="MA" data="Boston"/>
                      etc...
                 </fx:Array> 
              </mx:ArrayCollection>
      

      That may be fine for an example, but I think this is a rare situation.  Most of the time I would image that the arrayCollection would be created and populated on the fly in ActionScript!  How can I do this?

       

      Thanks in advance for any help or advice anyone can give!

       

      Matt

        • 1. Re: How Do You Populate A Spark List Control With An Array?
          UbuntuPenguin Level 4

          In your "relevant MXML" you have left out relevant data, especially the part where you set the dataProvider on the List.  It should look something like this

           

          <s:List dataProvider="{itemCollection}" .....

           

          If there is anything easy and anything that is well documented in the Flex world it is the setting of a dataprovider on a List.

          • 2. Re: How Do You Populate A Spark List Control With An Array?
            phaseblue Level 1

            UbuntuPenguin,

             

            Thanks for your help!

             

            I did forget one important line of code:

            knockingList.dataProvider = pendingArray;
            

            The reason I didn't set the dataProvider for the list in the MXML was because the Adobe help page I read didn't do that.  It was for setting the dataProvider in ActionScript, which is what I'm trying to do.  I don't want to use data binding because the original array is refreshed often and there is one function called by an event that re-declares the arrayCollection each time, populates it with the array, and then sets it as the list's dataprovider.  If I use data binding, then I will probably have to declare the arrayCollection as a global variable and then I'll have to write 100 or so extra lines of code to addItem(), removeItem(), sort(), etc...  It just seems like too much overhead.

            • 3. Re: How Do You Populate A Spark List Control With An Array?
              UbuntuPenguin Level 4

              In your post it seemed like you were trying to take care of many considerations at once: optimization, design, architecture.  I would suggest you get something up and running and then worry about everything else.

               

              ...

              If I use data binding, then I will probably have to declare the  arrayCollection as a global variable and then I'll have to write 100 or  so extra lines of code to addItem(), removeItem(), sort(), etc...  It  just seems like too much overhead.

              ...

               

              I believe you may have some misconceptions about databinding in general.  You won't have to make it a global variable and you certainly won't need an extra 100 lines of code.  If you did this forum would be a very , very quiet place.

               

              ...

              I don't want to use data binding because the original array is refreshed  often and there is one function called by an event that re-declares the  arrayCollection each time, populates it with the array, and then sets  it as the list's dataprovider.

              ...

               

              That is the beauty of the ArrayCollection, it can handle the updates to its source Array. I don't know if you need to redeclare the ArrayCollection, resetting the source to the new Array allows everyone involved to keep their references so you don't have to worry about any "spooky" stuff going on.

              • 4. Re: How Do You Populate A Spark List Control With An Array?
                phaseblue Level 1

                Thanks again UbuntuPenguin!

                 

                I apologize earlier for going off on a rant.  I was just tired and working on another problem, then suddenly I had to go off on a tangent and learn how to populate a Spark datalist control.

                 

                I did finally get it working (it was something else entirely that was causing the array not to get populated), however, I will take your advice and try to get it working with data binding.

                 

                Thanks again!

                 

                Matt