apache-flexactionscriptrandomflexbuilderxmllist

Random Picking an XMLList menuitem


i have this XMLList:

<fx:XMLList id="Generic List" xmlns="">
        <menuitem label="First entry" url="www.aaa.com"/>
        <menuitem label="Second entry" url="www.bbb.com"/>
        <menuitem label="Third Entry" url="www.ccc.com"/>
</fx:XMLList>

I want to randomly select in my combobox (code not displayed) one of these menu item picked randomly. I'm using Flex Builder 4,6.

Thanks for your help.


Solution

  • Try this:

    <?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">
    
    <fx:Declarations>
        <fx:XMLList id="genericList" xmlns="">
            <menuitem label="First entry" url="www.aaa.com"/>
            <menuitem label="Second entry" url="www.bbb.com"/>
            <menuitem label="Third Entry" url="www.ccc.com"/>
        </fx:XMLList>
    </fx:Declarations>
    
    <fx:Script>
        <![CDATA[
            import mx.collections.XMLListCollection;
        ]]>
    </fx:Script>
    
    
    <s:ComboBox 
        id="cbEntry" 
        dataProvider="{new XMLListCollection(genericList)}" 
        labelField="@label" 
        creationComplete="{cbEntry.selectedIndex = Math.floor(cbEntry.dataProvider.length * Math.random());}"/>
    
    </s:Application>