在Flex中使用XML填充组合框

时间:2012-12-26 22:11:07

标签: xml flex combobox httpservice

我正在尝试使用XML文件中的国家/地区填充组合框。不幸的是,组合框没有被填满。我该如何解决这个问题?提前谢谢!

这是我的代码:

protected function navigatorcontent2_creationCompleteHandler(event:FlexEvent):void
{
    fillCboCountries.addEventListener(ResultEvent.RESULT, fillCombobox);    
    fillCboCountries.send();        
}


protected function fillCombobox(event:ResultEvent):void
{           
    cboCountries.dataProvider=event.result.global.countryItem;              
}

<fx:Declarations>
<s:HTTPService id="fillCboCountries" url="https://marnixcoosemans2013.dreamhosters.com/scripts/countries_select.php"/>  
</fx:Declarations>  

<s:ComboBox id="cboCountries" x="10" y="414" width="173" labelField="countryLabel"/>

3 个答案:

答案 0 :(得分:1)

您的代码没有错误。我已经将它粘贴到我的空项目中,我在组合框中看到了你的字段。

我唯一改变的是http,尽管是https。

所以问题出在数据来源,而不是源代码!不用https就试试。

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" 
           minWidth="955" minHeight="600" 
           creationComplete="navigatorcontent2_creationCompleteHandler(event)">

<fx:Declarations>
    <s:HTTPService id="fillCboCountries" url="http://marnixcoosemans2013.dreamhosters.com/scripts/countries_select.php"/>  
</fx:Declarations>  

<fx:Script>
    <![CDATA[
        import mx.events.FlexEvent;
        import mx.rpc.events.ResultEvent;

        protected function navigatorcontent2_creationCompleteHandler(event:FlexEvent):void
        {
            fillCboCountries.addEventListener(ResultEvent.RESULT, fillCombobox);    
            fillCboCountries.send();        
        }

        protected function fillCombobox(event:ResultEvent):void
        {           
            cboCountries.dataProvider=event.result.global.countryItem;              
        }
    ]]>
</fx:Script>

<s:ComboBox id="cboCountries" x="10" y="414" width="173" labelField="countryLabel"/>
</s:Application>

答案 1 :(得分:0)

如果不知道服务调用的XML响应是什么样的,我唯一可以建议的是将HTTPService上的resultFormat设置为“e4x”:

<s:HTTPService id="fillCboCountries"
    url="https://marnixcoosemans2013.dreamhosters.com/scripts/countries_select.php"
    resultFormat="e4x" />

这告诉HTTPService响应是XML,并且您希望使用e4x表达式获取数据。

答案 2 :(得分:0)

谢谢大家的帮助。

我通过将代码复制到新项目中解决了这个问题。不幸的是,我不知道原始项目出了什么问题。