Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flex Hero:加载XML数据是可行的,但列表并没有更新_Xml_Apache Flex_Mobile_Dataprovider - Fatal编程技术网

Flex Hero:加载XML数据是可行的,但列表并没有更新

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

我有一个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数据正在从以下位置加载ok:


但不幸的是,这份名单仍然是空的

我有一种感觉,这里少了一件小事,也许应该把一个事件发送到列表中?我已经试着重新分配它的数据提供者,正如你在上面看到的,但它对我没有帮助

谢谢大家!!亚历克斯

你试过:

public function srvResult(event:ResultEvent):void 
{      myColl.source = event.result.pref.user.money as ArrayCollection;
       myList.dataProvider = myColl;
 }

您的代码中有几个问题:

  • event.result已经指向XML的根节点,因此不需要调用event.result.pref
  • 您应该使用@语法来获取XML属性值
  • 如果要从内联ItemRenderer访问标签函数,则无需将其设置为静态。您应该使用outerDocument属性。最佳实践是创建一个新的ItemRenderer mxml,并通过事件与其父级通信
  • 以下代码应该可以工作:

    <?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>