Expand my Community achievements bar.

Java Collections and AS Objects

Avatar

Level 1
Hi,



I tried the following for Remote Services:



MXML File:

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

<mx:Application xmlns:mx="
http://www.adobe.com/2006/mxml"


layout="vertical" cornerRadius="2"
backgroundGradientColors="[#b0bdf7, #b0bdf7]">

<!--

<mx:RemoteObject id="c4cast" destination="c4cast"

fault="handleError(event)">



<mx:method name="getUser" result="handleUserLogin(event)"
/>

</mx:RemoteObject>

-->

<mx:RemoteObject id="test" destination="test"

fault="handleError(event)">



<mx:method name="getUsers"
result="handleUserLogin(event)" />

</mx:RemoteObject>

<mx:Script>

<![CDATA[

import mx.rpc.events.ResultEvent;

import mx.events.ListEvent;

import flash.events.MouseEvent;

import mx.controls.Alert;



import mx.rpc.events.ResultEvent;

import mx.rpc.events.FaultEvent;

import mx.collections.ArrayCollection;

import mx.controls.Alert

//import com.cognizant.c4cast.*;

import com.test.*;

[Bindable]

public var loggedInUser:TestUser = null;

var userCollection:Array = null;





public function handleError(event:FaultEvent):void{

Alert.show("Error during Remote to C4CastService
:"+event.toString());

}





public function handleUserLogin(event:ResultEvent):void

{

var res:ArrayCollection = event.result as ArrayCollection;

Alert.show(""+res.length);

if(res.length >= 1){

loggedInUser = res[0];

Alert.show(""+loggedInUser.applications[0]);

//loginSuccess();

}else{

Alert.show("Login Failed !!!["+res.length+"]");

}



}



public function loginSuccess(){

this.logOut.label = "Log Out";

mainView.visible = true;

this.useridLabel.visible = false;

this.pwdLabel.visible = false;

this.userid.visible = false;

this.password.visible = false;

}



private function logInHandler (event:MouseEvent):void

{

this.test.getUsers.send();

}



private function selectionChanged (event:ListEvent):void

{

var selectedItem = appList.selectedLabel;

AppLabel.text = "Application Chosen is: '"+selectedItem+"'";

}



]]>

</mx:Script>

<mx:ApplicationControlBar dock="true"
creationComplete="this.test.getUsers.send()">

<mx:Label

text="See-4-Cast"

fontFamily="Verdana" fontWeight="bold" fontSize="12"

/>

<mx:Spacer width="100%" id="spacer01"/>

<mx:Text id="useridLabel" text="User Id:"
textAlign="right" fontWeight="bold"/>

<mx:TextInput width="98" id="userid" maxChars="10"
text="anandbn"/>

<mx:Text id="pwdLabel" text="Password:" textAlign="right"
fontWeight="bold"/>

<mx:TextInput width="98" id="password" maxChars="10"
displayAsPassword="true" text="password"/>

<mx:Spacer width="100%" id="spacer02"/>

<mx:Button label="Log In" id="logOut"
labelPlacement="right"

enabled="true" click="logInHandler(event)"/>

</mx:ApplicationControlBar>

<mx:ViewStack id="mainView" width="688" height="334"
visible="false">

<mx:TabNavigator x="10" y="10" width="100%"
height="100%">

<mx:Canvas label="Forecast Info" width="100%"
height="100%">

<mx:Label x="30" y="32" text="Choose Application:"
width="119"/>

<mx:ComboBox id="appList" enabled="true"

left="157" top="30" change="selectionChanged(event)"
selectedIndex="0">

</mx:ComboBox>

<mx:Label id="AppLabel" x="30" y="74" text="Application
Chosen is:" width="351" height="144"/>

<mx:Button x="340" y="30" label="Refresh"/>

</mx:Canvas>

<mx:Canvas label="Contractor Hours" width="100%"
height="100%">

</mx:Canvas>

</mx:TabNavigator>

</mx:ViewStack>



</mx:Application>




AS Object definitions

package com.test

{

import mx.collections.ArrayCollection;



[Managed]

[RemoteClass(alias="com.flex.test.model.TestUser")]

public class TestUser

{

public var name:String;

public var applications:ArrayCollection;

}

}



package com.test

{

[Managed]

[RemoteClass(alias="com.flex.test.model.TestApplication")]

public class TestApplication

{

public var name:String;

}

}




Java Object definitions:

public class TestUser {



private String name;

private List<TestApplication> applications = new
ArrayList<TestApplication>();

...

}



public class TestApplication {



private String name;

...

}




Issue Description:




var res:ArrayCollection = event.result as ArrayCollection;

Alert.show(""+res.length);

if(res.length >= 1){

loggedInUser = res[0];


Alert.show(""+loggedInUser.applications[0]);







The type of object I'm expecting is TestApplication but it
shows up as "object Object". I am not sure why the object in the
List is not getting converted to the correspong AS Object.
3 Replies

Avatar

Level 2
Ok try this, add a toString method to your TestApplication.as
and run your application again, it should now who you the result of
the toString method.



What happens if you try to cast the Object to
TestApplication?:



var res:ArrayCollection = event.result as ArrayCollection;

var test:TestApplication = TestApplication(res.getItem(0));

Alert.show(test);



Let me know if it works

Avatar

Level 1
toString did not help. Here's what I have for
TestApplication.as






package com.test

{

[Managed]

[RemoteClass(alias="com.flex.test.model.TestApplication")]

public class TestApplication

{

public var name:String;

public function toString():String{

return this.name;

}

}



}




Deliberate type cast to TestApplication did work but I do not
want to do it that way.

Avatar

Level 2
How did you test the toString method?



I suggest this:
Alert.show(""+loggedInUser.applications[0].toString());



You said the Casting did work but you don't want it that way?
Well what do you want then?