简体   繁体   中英

Creating a list from XML using Actionscript 3.0

Here is my XML http://pastie.org/5413932

Basically I want to list the names of the venues in a list box using the list component in AS3! I have had many attempts at this without any luck.

I already have the code to load in the XML etc here http://pastie.org/5413941 I have tried using list.addItem({label:myXML.venue.name}); in a function which does list the names of the venues but for some reason I cannot select them one by one when I run the swf, I can only select the first one.

Im a newbie to AS3

Thanks in advance :)

It seems that you have asked about this xml a question lately - so as this time we can see the XML structure then:

var venueNames:XMLList = myXML.venue.name;//here you have all name's nodes
if(venueNames && venueNames.length() > 0)
{
    for each (var name:XML in venueNames) 
    {
        trace(name);

        list.addItem({label:name.toString()});
    }
}

this is how you register clicks on the items on the list:

list.addEventListener(ListEvent.ITEM_CLICK, nameSelectedHandler, false, 0, true);

this is what the event may contain:

[ListEvent type="itemClick" bubbles=false cancelable=true columnIndex=0 rowIndex=0 index=0 item=[object Object]]

the item property of that event is what ever object was placed through the addItem method, as you can see there is also index property - you could use it to identify which item was clicked. Then you can get the venue node from XML as follows:

myXml.venue[e.index];//where e is the ListEvent

you can wrap it with a function and than use it elsewhere like in the following example in handler for clicking on submit button:

    protected function onSubmit(e:MouseEvent):void
    {
        if(list.selectedIndex != -1)
        {
            trace(findVenueByIndex(list.selectedIndex));
        }
        else
        {
            trace("Nothing selected");
        }
    }

    protected function findVenueByIndex(index:int = 0):XML
    {
        return myXML.venue[index];
    }

regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM