通过RemoteObject组件从服务端返回List对象

1、Java类方法源码

public List listUsers(){
	List list = new ArrayList();
	
	User u1 = new User("uid1", "pwd1");
	User u2 = new User("uid2", "pwd2");
	User u3 = new User("uid3", "pwd3");
	
	list.add(u1);
	list.add(u2);
	list.add(u3);
	
	return list;
}

 

2、mxml文件的源码

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init()">
	<mx:Script>
		<![CDATA[
			private function call():void{
				var o:Operation = ro.getOperation("listUsers") as Operation;
				o.addEventListener(ResultEvent.RESULT, resultEvent);
				o.send();
			}
			
	        private function resultEvent(event:ResultEvent):void{ 
	            try{
	            	var s:String = "";

					//Java的List对象 转换成 AS的ArrayCollection对象
	            	var list:ArrayCollection = event.result as ArrayCollection;
	            	
					//指定ComboBox组件的数据来源
	            	combo1.dataProvider = list;
	            	
					//循环访问ArrayCollection对象里的每一个元素,元素类型为User
	            	for(var i:int=0;i<list.length;i++){
	            		var u:User = list.getItemAt(i) as User;
	            		s += u.username + ", " + u.password + "\n";
	            	}

	            	txt1.text = s;
	            }catch(e:Error){
	            	txt1.text = e.message;
	            }
	        }
	        
	        private function closeHandler(event:Event){
	        	var c:ComboBox = event.target as ComboBox;
	        	var s:String = c.selectedItem.username + ", " + c.selectedItem.password;
	        	Alert.show(s);
	        }

		]]>
	</mx:Script>
	
	<mx:RemoteObject id="ro" destination="remoteService" fault="faultEvent(event)"/> 

	<mx:Button x="27" y="28" label="Load" click="call()"/>
	<mx:TextArea x="27" y="58" width="450" height="143" id="txt1"/>
	
	<mx:ComboBox x="27" y="209" id="combo1" labelField="username" close="closeHandler(event)"/>
	
</mx:Application>

 

你可能感兴趣的:(C++,c,工作,C#,Adobe)