我有这个Flex 4.5(Burrito)移动项目:
它由2个文件组成 - TextXML.mxml:
<?xml version="1.0" encoding="utf-8"?>
<s:MobileApplication
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:components="spark.components.*"
firstView="views.Home">
</s:MobileApplication>
和带有1个按钮,1个列表和1个HTTPService的Home.mxml:
<?xml version="1.0" encoding="utf-8"?>
<s:View
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:components="spark.components.*"
title="Home">
<fx:Script>
<![CDATA[
import mx.collections.*;
import mx.events.*;
import mx.rpc.events.*;
import mx.utils.*;
import spark.events.*;
[Bindable]
public var myColl:XMLListCollection = new XMLListCollection();
public function srvResult(event:ResultEvent):void {
trace(ObjectUtil.toString(event.result));
myColl.source = event.result.pref.user.money;
myList.dataProvider = myColl;
}
public static function myLabelFunc(item:Object):String {
return item.yw;
}
public static function myMessageFunc(item:Object):String {
return item.max;
}
]]>
</fx:Script>
<fx:Declarations>
<s:HTTPService
id="httpSrv"
url="http://preferans.de/user-xml.php?id=OK123195454365"
resultFormat="e4x"
result="srvResult(event)"
fault="trace(event.fault.message)" />
</fx:Declarations>
<s:navigationContent>
<s:Button label="Load XML" click="httpSrv.send()"/>
</s:navigationContent>
<s:List id="myList"
top="0" bottom="0" left="0" right="0"
dataProvider="{myColl}">
<s:itemRenderer>
<fx:Component>
<s:MobileIconItemRenderer
labelFunction="Home.myLabelFunc"
messageFunction="Home.myMessageFunc" >
</s:MobileIconItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:List>
</s:View>
当我在调试器中运行我的应用程序时(以便我可以在控制台中看到跟踪输出)并单击“加载XML”按钮,然后我看到XML数据从my PHP script加载正确:< / p>
<pref>
<user id="OK123195454365" first_name="Dmitry"
city="Moscow" money="8815" medals="1">
<money yw="2011-01" max="8815" user="8815"/>
<money yw="2010-52" max="6380" user="1545"/>
<money yw="2010-51" max="8797" user="2094"/>
<money yw="2010-50" max="8446" user="2080"/>
</user>
</pref>
但不幸的是,列表仍然是空的。
我觉得这里缺少一件小事,也许应该将一个事件发送到List?我已尝试重新分配其dataProvider,如上所示,但它对我没有帮助......
谢谢!亚历
答案 0 :(得分:2)
您的代码中存在以下几个问题:
以下代码应该有效:
<?xml version="1.0" encoding="utf-8"?>
<fx:Script>
<![CDATA[
import mx.collections.*;
import mx.events.*;
import mx.rpc.events.*;
import mx.utils.*;
import spark.events.*;
[Bindable]
public var myColl:XMLListCollection = new XMLListCollection();
public function srvResult(event:ResultEvent):void {
var xml:XML = event.result as XML;
myColl = new XMLListCollection(new XMLList(xml.user.money));
}
public function myLabelFunc(item:Object):String {
return item.@yw;
}
public function myMessageFunc(item:Object):String {
return item.@max;
}
]]>
</fx:Script>
<fx:Declarations>
<s:HTTPService
id="httpSrv"
url="test.xml"
resultFormat="e4x"
result="srvResult(event)"
fault="trace(event.fault.message)" />
</fx:Declarations>
<s:navigationContent>
<s:Button label="Load XML" click="httpSrv.send()"/>
</s:navigationContent>
<s:List id="myList"
top="0" bottom="0" left="0" right="0"
dataProvider="{myColl}">
<s:itemRenderer>
<fx:Component>
<s:MobileIconItemRenderer
labelFunction="{outerDocument.myLabelFunc}"
messageFunction="{outerDocument.myMessageFunc}" >
</s:MobileIconItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:List>
答案 1 :(得分:1)
当只有1个子节点时,由于某种原因,Flex不再将其视为数组,因此您必须直接引用该节点。
答案 2 :(得分:0)
你有没有尝试过:
public function srvResult(event:ResultEvent):void
{ myColl.source = event.result.pref.user.money as ArrayCollection;
myList.dataProvider = myColl;
}