简体   繁体   中英

list component as3

My list component for some reason is not displaying the data being pulled from an xml file Here is the code even though when i do a trace over "question1" Array i get the values. Could any one be able to tell me what am i doing wrong here?

import fl.controls.List;
    import fl.data.DataProvider;
    var myXML:XML;
    var list:List = new List  ;// new list item
        var question1:Array = new Array();
        var myLoader:URLLoader = new URLLoader();
        myLoader.load(new URLRequest("quiz1.xml"));
        myLoader.addEventListener(Event.COMPLETE, processXML);
        function processXML(e:Event):void
        {
         myXML = new XML(e.target.data);
         list.setSize(200,200);
         var xpos = (stage.stageWidth / 2) - (list.width / 2);
         var ypos = (stage.stageHeight / 2) - (list.height / 2);
         list.move(xpos,ypos);

         for (var i:int=0; i<myXML.*.length(); i++)
         {

          question1.push(myXML.questions[i].@idno);
          trace(question1);

         }
         list.dataProvider=new DataProvider(question1);
         addChildAt(list,0);
        }

When accessing an xml property, the return value is typed as XMLList which fl.data.DataProvider doesn't handle when building its internal array. Give this a try:

question1.push(myXML.questions[i].@idno.toString());

Another option is to let DataProvider build the data right from the xml but you'll likely need a labelField or labelFunction, try something like this:

list.dataProvider=new DataProvider(myXML);
list.labelField = "idno";

Also, you may want to access the question node in a item click handler in which case you can build your source array like so:

question1.push({
    label:myXML.questions[i].@idno.toString(),
    data:myXML.questions[i]
});

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