如何在Flex 4中提供Spark列表中项目的链接?

时间:2011-03-04 20:40:13

标签: flex flex4

<s:List id="lst"
            labelField="@label"
            change="lst_changeHandler(event)"
            horizontalCenter="0" verticalCenter="0">
        <s:dataProvider>
            <s:XMLListCollection>
                <fx:XMLList xmlns="">
                    <node label="One" />
                    <node label="Two" />
                    <node label="Three" />
                    <node label="Four" />
                    <node label="Five" />
                    <node label="Six" />
                    <node label="Seven" />
                    <node label="Eight" />
                    <node label="Nine" />
                </fx:XMLList>
            </s:XMLListCollection>
        </s:dataProvider>
    </s:List>

protected function lst_changeHandler(event:IndexChangeEvent):void
            {
               Alert.show(event.target.selectedItem); 

        }

我想用超链接扩展列表。例如,在每个节点中我也应该有一个href属性。然后我需要将用户重定向到所选项目。我知道Flash有URLRequest类。

我现在遇到的一个小问题是获取所选项目。它可能是因为dataprovider是xml并且我还没有完成正确的转换。或者也许比我更开明的人可以帮助我。

2 个答案:

答案 0 :(得分:1)

<fx:Script>
    <![CDATA[
        import flash.net.navigateToURL;

        import spark.events.IndexChangeEvent;

        protected function lst_changeHandler(event:IndexChangeEvent):void
        {
            navigateToURL(new URLRequest(lst.selectedItem.@url));
        }

    ]]>
</fx:Script>

<s:List id="lst"
        labelField="@label"
        change="lst_changeHandler(event)"
        horizontalCenter="0" verticalCenter="0"
        >
    <s:dataProvider>
        <s:XMLListCollection>
            <fx:XMLList xmlns="">
                <node label="One" url="www.internet.com" />
                <node label="Two" url="www.internet2.com" />
                <node label="Three" url="www.internet3.com" />
                <node label="Four" url="www.bla.com" />
            </fx:XMLList>
        </s:XMLListCollection>
    </s:dataProvider>
</s:List>

答案 1 :(得分:0)

 var item:XML = event.target.selectedItem as XML; 
                   var label:String = item.@label; 
                   var url:String = item.@url; 
                   if(url != null) {
                    var ur:URLRequest = new URLRequest(url); 
                    navigateToURL(ur); 
                   }

真的很简单!