Flex Hero:加载XML数据是可行的,但列表并没有更新
我有一个Flex 4.5(Burrito)移动项目: 它由2个文件组成-TextXML.mxml:Flex Hero:加载XML数据是可行的,但列表并没有更新,xml,apache-flex,mobile,dataprovider,Xml,Apache Flex,Mobile,Dataprovider,我有一个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.compon
<?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数据正在从以下位置加载ok:
但不幸的是,这份名单仍然是空的
我有一种感觉,这里少了一件小事,也许应该把一个事件发送到列表中?我已经试着重新分配它的数据提供者,正如你在上面看到的,但它对我没有帮助
谢谢大家!!亚历克斯你试过:
public function srvResult(event:ResultEvent):void
{ myColl.source = event.result.pref.user.money as ArrayCollection;
myList.dataProvider = myColl;
}
您的代码中有几个问题:
<?xml version="1.0" encoding="utf-8"?>
当只有一个子节点时,由于某种原因Flex不再将其视为数组,因此您必须直接引用该节点。这会导致编译错误“1067:隐式强制mx类型的值。集合:ArrayCollection到不相关的类型XMLList”。感谢您给出的好答案!我不明白一件事是如何工作的:myList有dataProvider=“{myColl}”,同时,每当从服务器加载XML时,myColl就会被分配一个新的已分配对象:myColl=new XMLListCollection(new XMLList(XML.user.money));它仍然有效吗?这是正确的方法吗?据我所记得的,框架自己为您处理这个问题。我不记得在哪里读到的,但是如果您使用keep生成的actionscript参数编译器,您将能够跟踪所有绑定的情况
<?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>