您的位置:首页 > 编程语言 > Java开发

java ArrayList 转成Flex ArrayCollection

2010-06-13 00:57 603 查看
1. server

package com.east.flex.serverpush;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.east.flex.serverpush.entity.Product;
/**
*
* @author East(张栋芳)
*
* 2010-6-13
*/
public class CreateProduct extends Thread {

private List<Product> productList = new ArrayList<Product>();

public CreateProduct() {
// this.start();
}

@Override
public void run() {

// while (true) {
//
// Product p = new Product();
// Random random = new Random();
// p.setMonth("Jan");
// p.setProfit(random.nextInt(1000));
// p.setExpenses(random.nextInt(500));
// p.setAmount(random.nextInt(300));
//
// productList.add(p);
// if (productList.size() == 15) {
// try {
// break;
// } catch (Exception se) {
// System.out.println(se.getMessage());
// }
// }
// System.out.println("Size is :" + productList.size());
// }
}

public List<Product> getProductList() {

for (int i = 0; i < 3; i++) {
Product p = new Product();
Random random = new Random();
p.setMonth("Jan" + i);
p.setProfit(random.nextInt(1000));
p.setExpenses(random.nextInt(500));
p.setAmount(random.nextInt(300));

productList.add(p);
}
return productList;
}

public Product getProduct() {
Product p = new Product();
Random random = new Random();
p.setMonth("Jan");
p.setProfit(random.nextInt(1000));
p.setExpenses(random.nextInt(500));
p.setAmount(random.nextInt(300));
return p;
}

public void setProductList(List<Product> productList) {
this.productList = productList;
}
}


2.client

<?xml version="1.0"?>
<!-- Simple example to demonstrate the LineChart and AreaChart controls. -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>import com.east.flex.entity.Product</mx:Script>
<mx:Script>
<!--[CDATA[

import mx.utils.ArrayUtil;
import mx.rpc.events.ResultEvent;
import mx.collections.ArrayCollection;
import mx.controls.Alert;

[Bindable]
private var p:Product;

private var array:ArrayUtil;

[Bindable]
private var expensesAC:ArrayCollection = null;

private function autoRefresh(event:MouseEvent):void
{
//serverPush.getProduct();
serverPush.getProductList();
}

private function resultHandler(event:ResultEvent):void
{
//处理多个值
trace(event.result);
//ArrayUtil.toArray();
linechart.dataProvider=event.result;

/*处理一个值
p= event.result as Product;
linechart.dataProvider= [
{ Month: "Otc", Profit: p.profit, Expenses: p.expenses, Amount: p.amount },
{ Month: "Jan", Profit: 500, Expenses: 800, Amount: 600 },
{ Month: "Fre", Profit: 300, Expenses: p.expenses, Amount: p.amount }];
*/
}
]]-->

</mx:Script>

<mx:RemoteObject id="serverPush" result="resultHandler(event)" destination="createProduct"/>

<!-- Define custom colors for use as fills in the AreaChart control. -->
<mx:SolidColor id="sc1" color="blue" alpha=".3"/>
<mx:SolidColor id="sc2" color="red" alpha=".3"/>
<mx:SolidColor id="sc3" color="green" alpha=".3"/>

<!-- Define custom Strokes. -->
<mx:Stroke id = "s1" color="blue" weight="2"/>
<mx:Stroke id = "s2" color="red" weight="2"/>
<mx:Stroke id = "s3" color="green" weight="2"/>

<mx:Panel title="LineChart and AreaChart Controls Example"
height="100%" width="100%" layout="absolute">

<mx:LineChart id="linechart" height="100%" width="1030"
paddingLeft="5" paddingRight="5"
showDataTips="true" x="0" y="0">

<mx:horizontalAxis>
<mx:CategoryAxis categoryField="month"/>
</mx:horizontalAxis>

<mx:series>
<mx:LineSeries yField="profit" form="curve" displayName="profit" lineStroke="{s1}"/>
<mx:LineSeries yField="expenses" form="curve" displayName="expenses" lineStroke="{s2}"/>
<mx:LineSeries yField="amount" form="curve" displayName="amount" lineStroke="{s3}"/>
</mx:series>
</mx:LineChart>

<mx:Legend dataProvider="{linechart}" x="1038" y="0" width="107"/>
<mx:Button label="AutoRefresh" x="1038" y="87" width="107" click="autoRefresh(event)"/>
</mx:Panel>
</mx:Application>


3. 配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<service id="remoting-service"
class="flex.messaging.services.RemotingService">

<adapters>
<adapter-definition id="java-object"
class="flex.messaging.services.remoting.adapters.JavaAdapter"
default="true" />
</adapters>

<default-channels>
<channel ref="my-amf" />
</default-channels>
<destination id="createProduct">
<properties>
<source>com.east.flex.serverpush.CreateProduct</source>
</properties>
</destination>

</service>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: