2 Replies Latest reply on Sep 17, 2006 7:51 PM by inlineblue

    Whats up with arrays

    arkcto
      Hi, I've posted a related topic on a different forum. Now, I keep getting into problems with converting arrays from data to actionscript/mxml.

      Please help.

      I want to populate a datagrid using an array which is a return value of a method of a remote object. (phew!)

      Here is my java object (the array is an array of these objects).

      public class Player {
      public String firstName;
      public String lastName;
      ......
      }

      I have an actionscript class corresponding to the Java Player class.

      package rpc.playerinfo
      {
      [RemoteClass(alias="rpc.playerinfo.Player")]
      [Bindable]
      public class Player {
      public var firstName:String;
      public var lastName:String;
      public function Player() { }
      }
      }


      The remote object destination that implements the method to send an array of these objects to tthe client.
      public class PlayerService {

      public Player[] getPlayersAsArray() {
      Player[] players = new Player[3];
      Player rahul = new Player("Rahul","Dravid");
      Player sachin = new Player("Sachin","Tendulkar");
      Player saurav = new Player("Saurav","Ganguly");
      players[0] = rahul;
      players[1] = sachin;
      players[2] = saurav;
      return players;
      }


      And I configured the destination PlayerService as :

      <destination id="PlayerService">
      <properties>
      <source>rpc.playerinfo.PlayerService</source>
      <scope>application</scope>
      </properties>
      </destination>


      My entire mxml appication code is here :

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" initialize="initApp()">

      <mx:Script>
      <![CDATA[
      import mx.controls.Alert;
      import mx.collections.ArrayCollection;
      import rpc.playerinfo.Player;
      import mx.utils.ArrayUtil;

      [Bindable]
      public var players:ArrayCollection;
      public var myPlayer:Player;

      function initApp() {
      players = new ArrayCollection(playMe.getPlayersAsArray());
      }
      ]]>

      </mx:Script>

      <mx:RemoteObject
      id="playMe"
      destination="PlayerService"
      showBusyCursor="true">
      <mx:method name="getPlayersAsArray">
      </mx:method>
      </mx:RemoteObject>

      <mx:DataGrid dataProvider="{players}" width="100%">
      <mx:columns>
      <mx:DataGridColumn dataField="firstName" headerText="FirstName"/>
      <mx:DataGridColumn dataField="lastName" headerText="LastName"/>
      </mx:columns>
      </mx:DataGrid>

      </mx:Application>

      Everytime I launch, I keep getting the following error message :

      TypeError: Error #1034: Type Coercion failed: cannot convert mx.rpc::AsyncToken@33dcbf1 to Array.

      Also, how does Flex know that the array is an array of Player objects ? (as opposed to an array of string or ints etc)

      I can't get this simple example to work (:


      Thanks so much for your help.
        • 1. Re: Whats up with arrays
          Renaun_Erickson Level 1
          Change:

          [Bindable]
          public var players:ArrayCollection;

          To:

          [ArrayElementType("rpc.playerinfo.Player")]
          [Bindable]
          public var players:ArrayCollection;
          • 2. Re: Whats up with arrays
            inlineblue Level 1
            A RemoteObject call is asynchronous. You do not get the result of a call from the return value. What it actually returns is a token object that represents the call. You have to listen to the "result" event for when the call completes. eg:

            <mx:method name="getPlayersAsArray" result="onGetPlayersResult(event)">

            private function onGetPlayersResult(event: ResultEvent): void
            {
            //The result data is found in event.result
            ...
            }

            Look in the docs for more details.