Flex Hero:加载 XML 数据有效,但列表未更新

发布于 2024-10-11 05:48:54 字数 3191 浏览 5 评论 0原文

我有这个 Flex 4.5 (Burrito) Mobile 项目:

alt text

它由 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>

和Home.mxml 带有 1 个按钮、1 个列表和 1 个 HTTPService:

<?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 数据是从我的PHP脚本加载正常:

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

但不幸的是列表仍然是空的。

我有一种感觉,这里缺少一件小事,也许应该将一个事件发送到列表?我已经尝试重新分配其 dataProvider,如您在上面看到的,但这对我没有帮助......

谢谢!亚历克斯

I have this Flex 4.5 (Burrito) Mobile project:

alt text

It consists of 2 files - the 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>

and the Home.mxml with 1 Button, 1 List and 1 HTTPService:

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

When I run my app in the debugger (so that I can see the trace output in the console) and click the "Load XML" button, then I see that XML data is loading ok from my PHP script:

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

But the List unfortunately stays empty.

I have a feeling that a minor thing is missing here, maybe an event should be sent to the List? I've tried reassigning its dataProvider as you can see above, but it doesn't help me...

Thank you! Alex

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七婞 2024-10-18 05:48:54

您的代码中有几个问题:

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

以下代码应该有效:

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

There are several issues in your code :

  1. event.result is already pointing to the root node of the XML, so you don't need to call event.result.pref.
  2. You should use the @ syntax to get XML attributes values.
  3. You don't need to make your label functions static if you want to access them from an inline ItemRenderer. You should use the outerDocument property. The best practice is to create a new ItemRenderer mxml and communicate with its parent via events.

The following code should work :

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

葬花如无物 2024-10-18 05:48:54

当只有 1 个子节点时,由于某种原因 Flex 不再将其视为数组,因此您必须直接引用该节点。

When there is only 1 child node, for some reason Flex no longer treats it as an array, so you have to reference the node directly.

别理我 2024-10-18 05:48:54

你有没有尝试过:

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

Have you try:

public function srvResult(event:ResultEvent):void 
{      myColl.source = event.result.pref.user.money as ArrayCollection;
       myList.dataProvider = myColl;
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文