Flex中ArrayCollection 与java中的ArrayList互转
这里说这是Flex用remoting传一个ArrayCollection到后台(后台也用ArrayCollection接参)之后如何转成ArrayList.
(具体操作如将Flex页面的DataGrid中的若干内容添加到DataBase中)
public ArrayList convertArrayCollection(ArrayCollection array){
ArrayList myObjectArray = new ArrayList();
ASTranslator ast = new ASTranslator();
ModuleVO myObject;
ASObject aso;
for (int i=0;i < array.size(); i++){
myObject = new ModuleVO ();
aso = new ASObject();
aso = (ASObject) array.get(i);
aso.setType("com.hospital.vo.ModuleVO");
myObject = (ModuleVO) ast.convert(aso, ModuleVO.class);
myObjectArray.add(myObject);
}
return myObjectArray;
}
Flex端VO:
package vo
{
[Bindable]
public class ModuleVO
{
public var num:Number;
public var MID:int;
public var MCode:String;
public var MCN:String;
public var MEN:String;
public var MState:int;
public function fill(obj:Object):void{
for(var i:* in obj){
this[i]=obj[i];
}
}
}
}
Java端ModuleVO:
package com.hospital.vo;
public class ModuleVO {
public Number num;
public int MID;
public String MCode;
public String MCN;
public String MEN;
public int MState;
public Number getNum() {
return num;
}
public void setNum(Number num) {
this.num = num;
}
public int getMID() {
return MID;
}
public void setMID(int mid) {
MID = mid;
}
public String getMCode() {
return MCode;
}
public void setMCode(String code) {
MCode = code;
}
public String getMCN() {
return MCN;
}
public void setMCN(String mcn) {
MCN = mcn;
}
public String getMEN() {
return MEN;
}
public void setMEN(String men) {
MEN = men;
}
public int getMState() {
return MState;
}
public void setMState(int state) {
MState = state;
}
}
Java取convertArrayCollection里面的数据:
public boolean addMV( ArrayCollection ModuleList) {
ArrayList al = this.convertArrayCollection(ModuleList) ;
for(Iterator i = al.iterator(); i.hasNext();) {
String MID = ((ModuleVO)i.next()).getMID()+"";
System.out.println(MID);
String sql = "insert into T_ModuleV (MVID, MID, MState) values " +
"("+MVID+", "+MID+", "+MState+") ; ";
System.out.println(sql);
}
}
将Flex端传过来的ArrayConllection转为Java端的ArrayList
public ArrayList convertArrayCollection(ArrayCollection array){
ArrayList myObjectArray = new ArrayList();
ASTranslator ast = new ASTranslator();
ModuleVO myObject;
ASObject aso;
for (int i=0;i < array.size(); i++){
myObject = new ModuleVO();
aso = new ASObject();
aso = (ASObject) array.get(i);
aso.setType("com.hospital.vo.ModuleVO");
myObject = (ModuleVO) ast.convert(aso, ModuleVO.class);
myObjectArray.add(myObject);
}
return myObjectArray;
}
其实传一个对象集合到后台的话直接在后台用ArrayList接参就可以(从java端传到Flex端也是一样),只不过ArrayCollection的vo对象要和后台中的bean对应起来(属性名一样,否则接不到值),需要注意的是Flex中vo对象中的属性要用public.
Flex端传给Java端一个ArrayCollection,Java端用ArrayList接收,之后循环遍历ArrayList,强转,就会取到Flex端传过来的值
For(int I = 0; i<arrayList.size();i++ ){
HashMap map = (HashMap)arrayList.get(i) ;
String A = map.get(“Flex端VO的字段名”).toString() ;
}