6 Replies Latest reply on Aug 4, 2009 1:01 PM by Gregory Lafrance

    Retrieve information from arrayCollection


      Hi everyone,


      i have a very simple problem, but i'm a new flex developper and i have no clue of what to do.

      i've retrieved information from a database using a webservice in coldfusion.

      in flex i retrieved the result of my query into a arrayCollection



      private var tabInfosDriver:ArrayCollection




      private function driverInfoHandler(event:ResultEvent):void

      tabInfosDriver=event.result as ArrayCollection;









      and if i give {tabInfosDriver} as dataprovider to some datagrid, the query result is shown properly, something like that.




      now i want to use someValue from header1 to populate a textField for example.

      i tried :

      label1.text = tabInfosDriver.getItemAt(1,0).toString()


      but it errored : index array out of bounds, and anyway i want to use the name of the header field, wich is the one of the database table to call it's value


      label1.text = tabInfosDriver.getItemAt("header1").value.toString()


      what can i do ? i'm sure this is easy to solve. i have looked all the methods from the arrayCollection object, but i can't figure wich one to use.


      please forgive me for my language, i do not speak english very often.



        • 1. Re: Retrieve information from arrayCollection
          Gregory Lafrance Level 6

          I do not fully understand exactly you are trying to do, but anyway getItemAt(1,0) won't work, do you mean getItemAt(1) or getItemAt(0)?


          Each column in the datagrid has a property headerText, so you might be able to use that somehow.


          Like I said, I'm not exactly sure what you want to do, please describe again.


          If this post answered your question or helped, please mark it as such.

          1 person found this helpful
          • 2. Re: Retrieve information from arrayCollection
            domflex Level 1

            ok,     thanks for answering, i'll try to be more clear.


            the getItemAt(n) doesn't intersest me. the index is no use for me because i don't know wich index refers to what.


            I need to retrieve the data from the arrayCollection knowing the name of the column (wich contains only one value).

            so, yes your idea of using the header text property is interesting, but not onto a datagrid but directly on the arrayCollection.


            in java, i would ask to retrieve the value from it's key, wich key would of been setted when building the array. And I do suppose that you can do this also with an flex arrayCollection


            i hope this helps you help me, cause i'm blocked here.


            thank you again.



            • 3. Re: Retrieve information from arrayCollection
              Gregory Lafrance Level 6

              You need an associative array:




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

              1 person found this helpful
              • 4. Re: Retrieve information from arrayCollection
                domflex Level 1

                Ok, associative arrays are what i need. I looked documentation and indeed it's what i'm describing.


                my problem isn't solve though, for i am not the one building the array. the array is built by sql server. then returned to the flex program whom

                parses it into a arrayCollection so it can use the information. so, unless i can build an associativeArray dynamically with the headers of my sql query columns (into the arrayCollection) as the keys, i can't use the associativeArray. and, if i can build one with the column names as  keys, there is surely a way to extract them otherwise.


                i've been thinking of another way to extract my data.


                if the output of the datagrid containing my query shows the way my arrayCollection is built, i mean if the first column in the datagrid is for example header1 and  myArrayCollection.getItemAt(1) = header1, there is a relation that with a little bit of code i can handle.


                though the output of the datagrid seems to be done alphabetically, wich means it is no use for me.



                if you can tell me what you think about these two ideas, i would be very glad. and i'd continue giving you points.


                thank you Greg



                • 5. Re: Retrieve information from arrayCollection
                  domflex Level 1

                  also, i've just thought that i'm the one in charge of the sql query so if giving aliases to my columns could be of any help

                  or if any solution could come from the sql query, please inform me.



                  • 6. Re: Retrieve information from arrayCollection
                    Gregory Lafrance Level 6

                    Often it is possible to either return data to the Flex app in standard object notation, or to reconfigure the data that way after it comes back to the Flex app in the result handler. The standard notation I'm referring to is:



                        {key1: "value 1", key2: "value2", key3: "value3"},

                        {key1: "value 1", key2: "value2", key3: "value3"},

                        {key1: "value 1", key2: "value2", key3: "value3"},

                        {key1: "value 1", key2: "value2", key3: "value3"}



                    So in this way the data is already structured as an associative array, because such an array is really just an ActionScript object.


                    So the above data should be accessible in two ways:


                    var tempVar:String = myObject.key1;

                    var tempVar:String = myObject[key1];    (might be myObject["key1"])


                    Incidentally, that "standard object notation" is also known as JSON, and there are libraries out there for handling JSON formatted data in Flex, PHP, etc. though you don't necessarily have to process it as JSON, that just makes it a bit more formal, and provides some additional APIs: