我的XML如下所示
<?xml version="1.0" encoding="utf-8"?>
<Images>
<Image imgID = "1" >
<imgName>"Angelina Jolie"</imgName>
<url>"Angelina Jolie.jpg"</url>
</Image>
<Image imgID = "2" >
<imgName>"Big B"</imgName>
<url>"Big B.jpg"</url>
</Image>
<Image imgID = "3" >
<imgName>"Brad Pitt"</imgName>
<url>"Brad Pitt.jpg"</url>
</Image>
<Image imgID = "4" >
<imgName>"Mohanlal"</imgName>
<url>"Mohanlal.jpg"</url>
</Image>
<Image imgID = "5" >
<imgName>"Prithviraj"</imgName>
<url>"Prithviraj.jpg"</url>
</Image>
<Image imgID = "6" >
<imgName>"Tom Cruise"</imgName>
<url>"Tom Cruise.jpg"</url>
</Image>
</Images>
在我的文件夹中,我有如图所示的XML。文件夹中有6个图像,名称作为URL给出。我在舞台上有一个组合框。它的名字是myCB。有一个图像持有者imgHolder_mc。
我有两个任务。
使用imageNames填充组合框。
根据所选的imageName,图像持有者中的图像必须更改。必须使用图片网址检索图片。
这是我到目前为止所做的事情
import fl.data.DataProvider;
myCB.prompt = "Select Image:";
var model_dp:DataProvider = new DataProvider(xmlData);
myCB.dataProvider = model_dp;
// Creates the variable(s) to load the XML externally
var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
// Ensuring that we do not prematurely start fiddling with the XML data until all of our data has been loaded
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
// We call our xmlLoader's load method
xmlLoader.load(new URLRequest("Images.xml"));
function LoadXML(e:Event):void {
xmlData = new XML(e.target.data);
trace(xmlData..imgName);
}
跟踪给了我
<imgName>"Angelina Jolie"</imgName>
<imgName>"Big B"</imgName>
<imgName>"Brad Pitt"</imgName>
<imgName>"Mohanlal"</imgName>
<imgName>"Prithviraj"</imgName>
<imgName>"Tom Cruise"</imgName>
请告诉我如何将其绑定到我的组合框并根据我在组合框中所做的选择选择图像。
答案 0 :(得分:2)
myCB.prompt = "Select Image";
var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
var xmlAry:Array = new Array();
var xmlURL:Array = new Array();
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest("Images.xml"));
function LoadXML(e:Event):void {
xmlData = new XML(e.target.data);
trace(xmlData..imgName.length());
for(var i:uint=0;i<xmlData..imgName.length();i++)
{
xmlAry.push(xmlData..imgName[i]);
xmlURL.push(xmlData..url[i]);
myCB.addItem( { label: xmlAry[i], data:i } );
myCB.addEventListener(Event.CHANGE, action);
}
}
function action (e:Event):void
{
var no:Number = Number(myCB.selectedItem.data);
trace(xmlURL[no]);
}