从远程http服务填充Datagrid

时间:2009-07-21 11:41:30

标签: flex

我正在尝试我的第一个flex应用程序。并且在将数据从xml http服务添加到datagid时遇到问题。

我的xml文件如下所示:

<players>

  <player>
    <name>test</name>
    <status>F</status>
    <claimed>1</claimed>
  </player>

  <player>
    <name>meta</name>
    <status>F</status>
    <claimed>1</claimed>
  </player>
</players>

首先,我尝试以原始方式填充数据,因此为HTTP服务创建了mxml标记,并添加了处理程序。

但很快我意识到主应用程序文件变得不可读(因为代码量很大),所以我决定以某种方式组织它。

所以决定用一个单独的类替换服务。

我的新代码如下:

MXML:

  <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
            layout="absolute" creationComplete="main()"  height="756" borderColor="#FFFFFF" width="950" top="10" left="10" horizontalAlign="left" verticalAlign="top" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#FCFCFC, #FCFCFC]">

        <mx:Panel width="900" height="727" layout="absolute" title="Игра ГО" horizontalAlign="center" horizontalCenter="0" top="10">
        <mx:Script>
            <![CDATA[

                import goclient.ListOfPlayers;
                import goclient.usersList;
                import goclient.Tester;
                import mx.controls.Alert;
                // And makes periodical requests to the server
                [Bindable]
                public var users:ListOfPlayers;
                [Bindable]
                public var test:Tester; 

                public function main():void{
                    test = new Tester();
                    users = new ListOfPlayers();



                }           

            ]]>
        </mx:Script>


            <mx:DataGrid doubleClickEnabled="true" dataProvider="{users.getPlayersList()}" 
                x="10" y="157" width="860" height="520" id="userList">
                <mx:columns>
                    <mx:DataGridColumn dataField="claimed" headerText="Was claimed" width="25"/>
                    <mx:DataGridColumn dataField="name" headerText="Name of the player"  />
                    <mx:DataGridColumn dataField="status" headerText="Status (Free or Busy)" />
                </mx:columns>
            </mx:DataGrid>

服务类:

ListOfPlayers.as

package goclient
{
    import flash.utils.Timer;
    import mx.controls.Alert;
    import mx.collections.ArrayCollection;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.http.mxml.HTTPService;

    public class ListOfPlayers
    {
        public var usersListService:HTTPService;
        private var minTimer:Timer = new Timer(100000, 0);  
        private var playersData:ArrayCollection;    
        private var person:currentPerson; 

        public function ListOfPlayers()
        {
            usersListService = new HTTPService();
            usersListService.url = "http://127.0.0.1:8000/go/active/";
            usersListService.addEventListener(ResultEvent.RESULT, resultHandler);
            //Alert.show("Here");
            sendData(); 
            //minTimer.addEventListener(TimerEvent.TIMER, sendData);
            //minTimer.start();

        }

        public function getResp():String
        {
            return "Resr";
        }

        public function resultHandler(event:ResultEvent):void
        {
            //person = new currentPerson(event.result.current.username, event.result.current.img, event.result.current.rank);
            playersData = event.result.players.player;
            Alert.show("resh");

        }
        public function sendData():void
        {
            usersListService.send();
        }

        public function getPlayersList():ArrayCollection
        {
            Alert.show(playersData.toString());
            return playersData;
        }

    }
}

问题是数据网格中没有显示任何内容

我只是一个初学者,所以请告诉我错误的课程

1 个答案:

答案 0 :(得分:2)

结果函数(在ListOfPlayers类中)应该给出播放器列表而不是调用webservice的函数。

您可以做的是在服务器类中添加它:

[Bindable]
public var playersData:ArrayCollection; 

并在您的视图中添加此变量和可绑定标记,并在main()中设置值添加此行: playersData = users.playersData;

然后datagrid dataprovider是“{playersData}”

这应该有效。但是对于XML列表,要知道你在树中的深度总是有点困难;)