English 中文(简体)
AS3: 如何使用 ActionScript3 访问 XML 列表中的数据?
原标题:AS3: How to access data in XMLListCollection using ActionScript3?

我使用 XML 列表聚合器作为火花 ComboBox, 受到此链接的启发

< a href=>http://blogblog.shortvission.com/index.cfm/2009/15/FlexAS3-Custom-ComboBox-for-ComboBox-for-Cordries-with-XML" rel=“notfollown noreferrerr>http://blog.shortvission.com/index.cfm/2009/15/FlexAS3-Custom-ComboBox-for-Cordries- with-XML

XML 列表集合在此定义 :

public class ComboBox_Country extends ComboBox {

    private var Country:XML=new XML(
        <countries>
         <country code="US" iso="840" label="United States" />
         <country code="CA" iso="124" label="Canada" />
         <country code="GB" iso="826" label="United Kingdom" />
                     ....
         <country code="ZM" iso="894" label="Zambia" />
         <country code="ZW" iso="716" label="Zimbabwe" />
        </countries>);

    public function ComboBox_Country() {
        dataProvider = new XMLListCollection(Country.children());
        labelField =  @label ;
    }

在 mxml 中喊叫 :

<mycomp:ComboBox_Country id="countryComboBox" width="100%"/>

当用户选择时, 我可以从 : < code> countryComboBox. sectedIndex 获得索引。 但是, 我需要国家的字符串, 我不知道如何从 XML 列表中提取索引。 当我在调试器中看到 :

""https://i.sstatic.net/UzWlC.png" alt="此处输入图像描述"/ >

s 表示用户选择的索引2(例如联合王国)。要输入调试器返回 United Kingdom 需要输入什么?

countryComboBox.Country.getItemAt(2)
countryComboBox.Country.getItemAt(2).label
countryComboBox.Country[2]
countryComboBox.Country.label.getItemAt(2)
etc...

徒劳无益。

最佳回答

ComboBox have a property selectedItem which you probably should use. In this case selectedItem would be XML object. Read how you can get data from XML objects here. In your case you can get label using

countryComboBox.selectedItem.@label
问题回答

我不完全确定您正确装入组合框是否正确, 您通常使用数据提供者( http:// help. adobe. com/ en_ US/ flex/ using/ WS70f0d54f063b9b081ac8d11247252e4a0-8000. html" rel=“ nofollow” > http://help. adobe. com/ en_ US/ flex/using/ WS70f0d54f063b9b083b081 aac8d11247252e4a0-8000. html

假设你正确显示数据,那么你就会非常接近

// Should give you the country object selected
var obj:Object = countryComboBox.selectedItem;

// You should also be able to use .code or .iso
return obj.label;

如果 obj. label 不工作, 您可以尝试 aobj[ 标签];





相关问题
Disable button tooltip in AS3

I want to disable the tooltip on certain buttons. The tooltip manager seems to be an all or nothing solution. Is it possible to disable the tooltip for just one or two buttons?

Multiple Remote call made simultenously

I was making multiple remote calls and they are done sequentially and when I am getting a result event back it s triggering calls to all the methods with ResultEvent as an argument . I am supposed to ...

Attaching a property to an event in Flex/AS3

I have a parameter that needs to be passed along with an event. After unsuccessful attempts to place it on the type by extending the class, I ve been advised in another SO question to write a custom ...

Clearing RSL in Cache

I have built a flex application which has a "main" project and it is assosciated with a few RSL s which are loaded and cached once i run my "main" application. The problem i am facing is that the ...

What s a good way of deserializing data into mock objects?

I m writing a mock backend service for my flex application. Because I will likely need to add/edit/modify the mock data over time, I d prefer not to generate the data in code like this: var mockData =...

AS3 try/catch out of memory

I m loading a few huge images on my flex/as3 app, but I can t manage to catch the error when the flash player runs out of memory. Here is the what I was thinking might work (I use ???? because i dont ...

热门标签