本文主要是介绍数据绑定(java类与ActionScript类绑定),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Book.as文件
package
{
//这里[Bindable]是可绑定的,[RemoteClass(alias="Book")]是和Java代码中的Book类关联起来。这样就可以利用类型转换来将Java对象转换为AS对象了。
[Bindable]
[RemoteClass (alias="book.Book")]
public class Book
{
public function Book()
{
}
public var isbn:String;
public var title:String;
public var price:int;
}
}
book.mxml文件
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:tw="http://www.servasoftware.com/2009/twaver/flex" minWidth="1024" minHeight="768" initialize="init()">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
private function init():void {
BookManager.getBookList();
BookManager.addEventListener(ResultEvent.RESULT,getBookList);
}
private function getBookList(event:ResultEvent):void {
var bookList:ArrayCollection = ArrayCollection(event.result);
var bookList_FlexData:ArrayCollection = new ArrayCollection();
for (var i:int = 0; i < bookList.length; i++) {
var books:Book = Book(bookList.getItemAt(i));
bookList_FlexData.addItem(books);
}
dataGrid.dataProvider = bookList_FlexData;
//dataGrid.dataProvider = event.result as ArrayCollection;
}
]]>
</fx:Script>
<fx:Declarations>
<s:RemoteObject id="BookManager" destination="bookDao" result="" showBusyCursor="true"/>
</fx:Declarations>
<s:Panel x="32" y="20" width="399" height="285" title="图书列表">
<mx:DataGrid x="0" y="0" width="379" height="243" id="dataGrid">
<mx:columns>
<mx:DataGridColumn dataField="isbn" headerText="编号"/>
<mx:DataGridColumn dataField="title" headerText="标题"/>
<mx:DataGridColumn dataField="price" headerText="价格"/>
</mx:columns>
</mx:DataGrid>
</s:Panel>
</s:Application>
Book类
package book;
public class Book {
private String isbn;
private String title;
private double price;
//get,set方法。。。
}
BookDao类
package book;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import book.Book;
public class BookDao {
public List<Book> getBookList(){
List<Book> list=new ArrayList<Book>();
for (int i = 0; i < 10; i++) {
Book book=new Book();
book.setIsbn("00"+i);
book.setTitle("Flex"+i);
book.setPrice(i);
list.add(book);
}
return list;
}
}
Remote-config.xml
<destination id="bookDao">
<properties>
<source>book.BookDao</source>
</properties>
</destination>
这篇关于数据绑定(java类与ActionScript类绑定)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!