1 Reply Latest reply on Apr 16, 2010 11:45 AM by Wladaaaaa

    ObjectProxy to ArrayCollection

    Wladaaaaa

      Hi to all.

      I am Vladimir from Serbia.

      I try to learn flex over the video tutorials and google. I am a new in flex and programing.

       

      Have a problem, and I need yours help.


      How to insert data from objectProxy to ArrayCollection.

      Here is my mxml code:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:views="views.*"
           creationComplete="initApp()" backgroundColor="#000000" >
           
           <mx:Style>
                .picturePanel, .thumb {
                     border-style: solid;
                     border-thickness:1px;
                     background-color:#0F0F0F;
                     border-color:#6F6F6F;
                     corner-radius:10;
                }
                .thumb {
                     padding-bottom:10;
                     padding-top:10;
                }
                .buttons {
                     fillColors:#0F0F0F, #6F6F6F;
                     fill-alphas: 1, 8;
                     color: #ffffff;
                     textRollOverColor:#FFFFFF; 
                        textSelectedColor:#FFFFFF;
      
                }
      
           </mx:Style>
           
           <mx:Script>
                <![CDATA[
                     import mx.controls.Alert;
                     import mx.utils.ObjectProxy;
                     import mx.rpc.events.ResultEvent;
                     import volueObjects.photo;
                     import mx.collections.ArrayCollection;
                     import mx.utils.ArrayUtil;
                     
                     [Bindable]
                     public var photoData:ArrayCollection = new ArrayCollection();
                     [Bindable]
                     private var photoRaw:ArrayCollection;
                     
                     // Result Handler - Fires when all data are coledted from xml
                     private function photosInHandler(event:ResultEvent):void
                     {
                          // Inserting data in the photoRaw array collection
                          if (event.result.photos == null)
                          {
                               photoRaw = new ArrayCollection();
                          }
                          else if (event.result.photos.image is ArrayCollection)
                          {
                               photoRaw = event.result.photos.image;
                          }
                          else if (event.result.photos.image is ObjectProxy)
                          {
                               photoRaw = new ArrayCollection(ArrayUtil.toArray(event.result.photos.image));     
                          }
                          //Alert.show(photoRaw.length.toString());
                          
                          // For loop
                          for ( var i:int=0; i<photoRaw.length; i++)
                          {     
                               // Creating valueObject
                               var tempPhoto:photo = new photo();
                               //Creating object and inserting data from array collection
                               var photoFromArray:Object = photoRaw.getItemAt(i);
                               // Inserting data in  tempPhoto value object
                               tempPhoto.filename = photoFromArray.filename;
                               tempPhoto.photographer = photoFromArray.photographer;
                               tempPhoto.category = photoFromArray.category;
                               tempPhoto.desc = photoFromArray.desc;
                               //Adding data in photoData Array
                               photoData.addItem(tempPhoto);
                          }     
                     }
                     // starting Http Service. Send data
                     private function initApp():void
                     {
                          photosIn.send();
                     }
                ]]>
           </mx:Script>
           <mx:HTTPService id="photosIn" method="get" showBusyCursor="true"
                url="selectData.php" result="photosInHandler(event)"  />
                <mx:LinkBar dataProvider="{Content}" fontSize="20" color="#FFFFFF" rollOverColor="#B5D2FA" x="350" y="10"/>
                     <mx:ViewStack id="Content" resizeToContent="true" x="101" y="118">
                          <views:home id="home" label="Home"/>
                          <views:gallery id="gallery" label="Gallery" photoData="{photoData}" />
           </mx:ViewStack>
      </mx:Application>
      

       

      Here is php code:

      <?php
      echo "<?xml version=\"1.0\" \n?>";
      
      $conn = mysql_connect("localhost", "root","");
      
      if (!mysql_select_db("mygallerydb")) {
          echo "Unable to select mygallerydb: " . mysql_error();
          exit;
      }
      $sql = "SELECT * FROM photos";
      
      $result = mysql_query($sql);
      
      $filename = $_GET["filename"];
      $photographer = $_GET["photographer"];
      $category = $_GET["category"];
      $desc = $_GET["desc"];
      
      /* print out your own XML */
      echo "<photos>\n";
      while( $row = mysql_fetch_object( $result ) )
      {
         echo "<image><filename>".$row->filename."</filename><photographer>".$row->photographer."</photographer>
                          <category>".$row->category."</category><desc>".$row->desc."</desc></image>";
      }
      echo "</photos>";
      
      ?>
      

       

      I getting one row with values:

       

      tempPhoto    volueObjects.photo (@19add2b1)   
          category    "".$row->category.""   
          desc    "".$row->desc.""   
          filename    "".$row->filename.""   
          photographer    "".$row->photographer.""  

       

       

       

      Can someone help me?

       

      thanks in advance