3 Replies Latest reply on Aug 2, 2009 9:26 AM by Andrewstbg86

    ArrayCollection quick reference

    Andrewstbg86 Level 1

      Hi,

       

      I have an arraycollection with a number of arrays inside: "Address,City,Zip Code, etc."

       

      I put these into an array like  ["Address","City",etc.]  would it be possible to retrieve the nth object of one of these like so

       

      MainArrayCollection[n].[otherarray[1]] would I would hope would give me the nth object of the City array.

       

      Is this possible to easily reference this or would I have to write out the reference for every case?

        • 1. Re: ArrayCollection quick reference
          chris.huston.t10 Level 3

          To get the nth item in an ArrayCollection, ust the getItemAt method:

           

          MainArrayCollection.getItemAt(n)[1];

           

          Here is an example (made with Flash Builder Beta)

           

          <?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/halo" minWidth="1024" minHeight="768">
              <fx:Script>
                  <![CDATA[
                      import mx.collections.ArrayCollection;
                      private var ac:ArrayCollection = new ArrayCollection([new Array('Address0', 'City0', 'Phone0'), new Array('Address1','City1','Phone1')]);
                     
                      private function getValue(n:uint,m:uint):void {
                          arrayValue.text = ac.getItemAt(n)[m];
                      }
                 
                  ]]>
                 
              </fx:Script>
              <s:Button click="getValue(0,1)" label="Get 2nd Element of First Array" x="20" y="20"/>
              <s:Button click="getValue(1,1)" label="Get 2nd Element of Second Array" x="20" y="50"/>
              <mx:Label id="arrayValue" x="20" y="100"/>
          </s:Application>

           

          Chris

          • 2. Re: ArrayCollection quick reference
            Andrewstbg86 Level 1

            --!!!!  Worked!  My Bad !!!!--

             

            Couldn't get it to work

             

            Each array in the collection that I have has a label like "Address" "City" "Country" for example.

             

            To handle this and not many If statements I made two arrays.  The first one contains the names of all these arrays

             

            var Array1:Array= ["Address", "City", "Country"]

             

            the second array just maps to getting the item in the first.

             

            How do I go about now getting the nth item in the ArrayCollection from the "Address" array for example without typing ArrayCollection[u].Address, instead is it possible to write ArrayCollection[u].[Array1[1]].  I'm trying to reduce having to make if statements for each and every argument for the first and second arrays.

            • 3. Re: ArrayCollection quick reference
              Andrewstbg86 Level 1

              Nevermind!  I changed the uint to the string representation and it worked.  Thanks!