遍历 XMLList 项目可能有点棘手,我建议ArrayCollection
使用 for in 循环(请参阅附加的代码示例)填充下拉选项,然后在更改事件上使用事件侦听器来解析和填充文本字段。
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" creationComplete="init()"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import spark.events.IndexChangeEvent;
[Bindable]private var ddList:ArrayCollection = new ArrayCollection();
[Bindable]private var output:String = "";
private var xbuilding:XMLList = new XMLList(
<building>
<room>
<name>Lobby</name>
<location>Some Place</location>
</room>
<room>
<name>Another Room</name>
<location>Some Other Place</location>
</room>
<room>
<name>Yet Another Room</name>
<location>The Final Place</location>
</room>
</building>);
protected function init():void
{
for each (var roomName:String in xbuilding.room.name){
ddList.addItem(roomName);
}
}
protected function dropDown_changeHandler(event:IndexChangeEvent):void
{
output = "name :"+xbuilding.room[event.newIndex].name+"\nlocation: "+xbuilding.room[event.newIndex].location;
}
]]>
</fx:Script>
<s:VGroup>
<s:DropDownList dataProvider="{ddList}" change="dropDown_changeHandler(event)"/>
<s:TextArea text="{output}" editable="false"/>
</s:VGroup>
</s:WindowedApplication>
要编辑或更改 XML 数据,只需将其视为对象数组,确保正确解析输入
xbuilding.room[selectedIndex] = new XMLList(newData)
有关更多信息,我推荐AS3Docs:使用 XML