1 Reply Latest reply on Sep 27, 2006 11:37 PM by jcraane

    Problem mapping a java.util.Collection to a actionscript ArrayCollection

    jcraane
      Hello,

      I am just starting to evaluate Flex and I am experiencing a problem mapping a java.util.Collection (HashSet) implementation to a Flex ArrayCollection. I have the following Java class:

      package nl.jamiecraane.adresboek.domain;
      public class ContactPersoon implements Serializable {
      private String voornaam;
      private Collection<String> hobbies = new HashSet<String>();
      public ContactPersoon() {
      }
      public ContactPersoon(String voornaam) {
      super();
      this.voornaam = voornaam;
      }
      public Collection<String> getHobbies() {
      return hobbies;
      }
      public void setHobbies(Collection<String> hobbies) {
      this.hobbies = hobbies;
      }
      public ContactPersoon() {
      }
      public String getVoornaam() {
      return voornaam;
      }
      public void setVoornaam(String voornaam) {
      this.voornaam = voornaam;
      }

      I have the following ActionScript class:

      package nl.jamiecraane.adresboek.domain
      {
      import mx.collections.ArrayCollection;

      [Managed]
      [RemoteClass(alias="nl.jamiecraane.adresboek.domain.ContactPersoon")]
      public class ContactPersoon
      {
      public var voornaam:String;
      public var hobbies:ArrayCollection;
      }
      }

      I have the following code in mxml:

      … some code omitted
      <mx:Script>
      <![CDATA[
      import nl.jamiecraane.adresboek.domain.ContactPersoon;
      import mx.rpc.events.ResultEvent;
      import mx.collections.ArrayCollection;
      import mx.data.DataService;

      public var ds:DataService;
      [Bindable]
      public var contactPersonen:ArrayCollection;

      public function initApp():void {
      contactPersonen = new ArrayCollection();
      ds = new DataService(“ContactPersonen”);
      ds.addEventListener(ResultEvent.RESULT, contactPersoonEventListener);
      ds.fill(contactPersonen);
      }

      public function contactPersoonEventListener(event:ResultEvent):void {
      for (var i:int = 0; i < contactPersonen.length; i++) {
      var cp:ContactPersoon = ContactPersoon(contactPersonen.getItemAt(i));
      trace(cp.achternaam);
      trace(cp.hobbies);
      }
      }
      ]]>
      </mx:Script>
      … some code omitted

      When I specify the following in the channel definition:

      <serialization>
      <legacy-collection>true</legacy-collection>
      </serialization>

      Everything works fine and the hobbies in the above trace statement (contactPersoonEventListener method) get printed. When I don’t specify the <legacy-collection> element (defaults to false) I get the following error:
      TypeError: Error #1034: Type Coercion failed: cannot convert mx.collections::ArrayCollection@38cceb1 to Array.

      Does anyone know what I am doing wrong?

      Thanks in advance.