9 Replies Latest reply on May 26, 2009 10:38 PM by DM277

    Array Collection

    Seglespaan

      Hi,

       

      I'm using the following code in conjunction with amfPHP to pull information from a database, this info is then (as far as I know) returned to flex as an array collection.

       

      What I'm looking to do is populate the list with the data within the array collection,

       

       

      Heres my code

       

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
         
          <mx:Script>
              <![CDATA[
                  import flash.net.*;
                  import mx.collections.ArrayCollection;
                  import mx.rpc.events.ResultEvent;
                 
                  public function connect():Object
                  {
                      var gw:NetConnection = new NetConnection();
                      gw.connect("http://localhost/amfphp/gateway.php");
                     
                      var res:Responder = new Responder(onResult, onFault);
                     
                      function onResult(responds:Object):void
                      {
                        trace(responds.serverInfo.initialData[0][1]);
                      }
                     
                      function onFault(responds:Object):void
                      {
                          for(var i in responds)
                          {
                              trace(responds[i]);
                          }
                      }
                     
                      gw.call("Test.getRecordset", res);
                     
                     
                      return res;
                  }
                 
                 
                  public function init()
                  {
                      connect();
                  }
                 
              ]]>
          </mx:Script>
         
         
         
          <mx:List x="75" y="113"></mx:List>
         
         
          
         
      </mx:Application>

        • 1. Re: Array Collection
          Seglespaan Level 1

          Forgot to say I can see the correct information has been passed to flex via the line

           

          trace(responds.serverInfo.initialData[0][1]);

           

          just not sure how to access this outwith the connect function

           

          Cheers

          • 2. Re: Array Collection
            DM277

            Try the following solution.

             

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
               
                <mx:Script>
                    <![CDATA[
                        import flash.net.*;
                        import mx.collections.ArrayCollection;
                        import mx.rpc.events.ResultEvent;
                     

                        private var arrResult:ArrayCollection = new ArrayCollection();


                        public function connect():Object
                        {
                            var gw:NetConnection = new NetConnection();
                            gw.connect("http://localhost/amfphp/gateway.php");
                           
                            var res:Responder = new Responder(onResult, onFault);
                           
                            function onResult(responds:Object):void
                            {
                              trace(responds.serverInfo.initialData[0][1]);

                               arrResult = responds.serverInfo.initialData;     
                            }
                           
                            function onFault(responds:Object):void
                            {
                                for(var i in responds)
                                {
                                    trace(responds[i]);
                                }
                            }
                           
                            gw.call("Test.getRecordset", res);
                           
                           
                            return res;
                        }
                       
                       
                        public function init()
                        {
                            connect();
                        }
                       
                    ]]>
                </mx:Script>
               
               
               
                <mx:List x="75" y="113" dataProvider = "{arrResult }">

                     

            <mx:itemRenderer>

             

             

            <mx:Component>

            <mx:VBox

             

            <mx:Label

             

             

             

            text="{data.Name}" />   //Here give the name of the value in the result

            </mx:VBox>

            </mx:Component>

            </mx:itemRenderer>

            </mx:List>

            </mx:Application>

            • 3. Re: Array Collection
              Seglespaan Level 1

              Thanks,

               

              i have tried this changing the line

               

              text="{data.Name}"

               

              to text="{data.category}"

               

              but the list still appears blank

               

              Cheers

              • 4. Re: Array Collection
                Seglespaan Level 1

                i've managed to get the list populating by making the

                 

                var arrResult bindable,

                 

                but the list populates as

                 

                1, field 1, field 2, field 3

                2, field 1, field 2, field 3

                3, field 1, field 2, field 3

                4, field 1, field 2, field 3

                5, field 1, field 2, field 3

                 

                etc,

                 

                 

                is there a way i can get it just to select the second field to put in the list and ingnore the other data!

                • 5. Re: Array Collection
                  Michael Borbor Level 4

                  Use the labelField property like this labelField="field2"

                  • 6. Re: Array Collection
                    Seglespaan Level 1

                    thanks,

                     

                    for some reason when i was using labelField="field1"

                     

                    nothing was changing, bu when I changed this to

                     

                    labelField="2"

                     

                    i got the desired result.

                     

                     

                    Here's another question, is there a way of only placing this label in the list if it doesn't already exist?

                     

                    similar to SELECT DISTINCT in mysql,

                     

                    or am i better off doing this within mySQL and only returning the distinct category names?

                    • 7. Re: Array Collection
                      Michael Borbor Level 4

                      Better do it in MySQL you reduce bandwith consumption.

                      • 8. Re: Array Collection
                        Seglespaan Level 1

                        I've modified my code as follows,

                         

                        the idea being that when the user selects a category from the list, the tile list is populated with the icons, I can get this to poulate with the icon reference but can't actually get the icons to appear. Also for some reason when i select business, the communications results come up and vice versa

                         

                         

                        <?xml version="1.0" encoding="utf-8"?>

                        <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" width="800" layout="absolute" creationComplete="init()">

                          

                            <mx:Script>

                                <![CDATA[

                                    import flash.net.*;

                                    import mx.collections.ArrayCollection;

                                    import mx.rpc.events.ResultEvent;

                                 

                        [Bindable]

                                    private var categoryResult:ArrayCollection;

                        [Bindable]

                                    private var iconResult:ArrayCollection;

                         

                                    public function getDistinct():Object

                                    {

                                    var gw:NetConnection = new NetConnection();

                                        gw.connect("http://localhost/amfphp/gateway.php");

                                      

                                        var res:Responder = new Responder(onResult, onFault);

                                      

                                        function onResult(responds:Object):void

                                        {

                                         

                                          categoryResult = new ArrayCollection(responds.serverInfo.initialData);  

                                          //trace(arrResult); 

                                        }

                                      

                                        function onFault(responds:Object):void

                                        {

                                            for(var i in responds)

                                            {

                                                trace(responds[i]);

                                            }

                                        }

                                      

                                       gw.call("Test.getDistinct", res);

                               

                                        return res;

                                    }

                                  

                                  

                                  

                                  

                                    public function getIcons(cats:String):Object

                                    {

                                   

                                    var gw2:NetConnection = new NetConnection();

                                        gw2.connect("http://localhost/amfphp/gateway.php");

                                      

                                        var res2:Responder = new Responder(onResult, onFault);

                                      

                                        function onResult(responds2:Object):void

                                        {

                                         

                                          iconResult = new ArrayCollection(responds2.serverInfo.initialData);  

                                          trace(iconResult); 

                                        }

                                      

                                        function onFault(responds2:Object):void

                                        {

                                            for(var i in responds2)

                                            {

                                                trace(responds2[i]);

                                            }

                                        }

                                       trace(cats);

                                       gw2.call("Test.getCategoryIcons", res2, cats);

                               

                                       return res2;

                                    }

                                  

                                  

                                  

                                    public function init()

                                    {

                                        getDistinct();

                                        getIcons("business");

                                    }

                                  

                                  

                                   public function changeIcons(cat)

                                   {

                                   trace(cat);

                                   getIcons(cat);

                                   iconTiles.dataProvider = iconResult;

                                   iconTiles.labelField = iconResult[0];

                                   iconTiles.iconField = iconResult[0];

                                  

                                   }

                                  

                                ]]>

                            </mx:Script>

                          

                          

                          

                        <mx:List id="categoryList"

                        width="150" x="75" y="113"

                        dataProvider = "{categoryResult}" labelField="1"

                        change="changeIcons(categoryList.selectedItem)" >

                         

                        </mx:List>

                         

                        <mx:TileList id="iconTiles"

                        x="223" y="113" height="156"

                        width="500"

                        labelField=""

                        ></mx:TileList>

                          

                          

                        </mx:Application>

                        • 9. Re: Array Collection
                          DM277 Level 2

                          Hi,

                          Could you please check whether you are passing the corect parameter "business" or "communication"?.CategoryList.SelectedItem will be returning an object not string.