2 Replies Latest reply on Jul 6, 2006 7:23 AM by djsodom

    Use of arrayCollection and resultEvent

    djsodom
      I'm having troubled binding parsed XML data. I've run out of searches for fixes.

      I'm building a trivia game widget using flex. The data source is an rss file such as:
      quote:

      <?xml version="1.0" encoding="UTF-8"?>
      <rss version="2.0" xmlns:dc=" http://purl.org/dc/elements/1.1/">
      <channel>
      <title>games Trivia Game</title>
      <link> http://www.trivionomy.com/play/games</link>
      <description>Trivionomy.com - Create, edit and play trivia.</description>
      <questions>
      <question>
      <title>Which pioneering game developer had a male to female sex change operation?</title>
      <answers>
      <answer>
      <title>David Jones</title>
      <correct>n</correct>
      </answer>
      <answer>
      <title>Jordan Mechner</title>
      <correct>n</correct>
      </answer>
      <answer>
      <title>Dan Bunten</title>
      <correct>y</correct>
      </answer>
      </answers>
      </question>
      <question>
      <title>What game did John Carmack create before Doom and Wolfenstein 3D?</title>
      <answers>
      <answer>
      <title>Commander Keen</title>
      <correct>y</correct>
      </answer>
      <answer>
      <title>Duke Nukem</title>
      <correct>n</correct>
      </answer>
      </answers>
      </question>
      </questions>
      </channel>
      </rss>



      My mxml code is:
      quote:


      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" initialize="feedRequest.send();">
      <mx:Script>
      <![CDATA[
      import flash.external.ExternalInterface;
      import mx.collections.ArrayCollection;
      import mx.rpc.events.ResultEvent;

      [Bindable]
      private var aryQuestion:ArrayCollection;

      //THIS IS NOT WORKING. MY Title does not get output.
      private function resultHandler(event:ResultEvent):void {
      aryQuestion = event.result.rss.channel.questions.question.getItemAt(0).title;
      ExternalInterface.call("alert", aryQuestion);
      //I've tried simpler things like "event.result.rss.channel.description" but that does not work.

      }


      private function answerGet(item:Object):String {
      if (item.title==undefined)
      {return null;}
      else
      {return item.title}
      }

      private function answerSelected(event:flash.events.Event):void {
      //change count
      //is it correct?
      if (answers.selectedItem.correct == "y") {
      ExternalInterface.call("alert", "correct");
      //update correct count
      }
      //change question
      }
      ]]>
      </mx:Script>
      <mx:HTTPService id="feedRequest" url=" http://localhost:3003/es/xml/tag/games" result="resultHandler(event)"/>
      <mx:Panel x="10" y="10" width="475" height="400" layout="absolute"
      title="Play. Trivia." id="box" styleName="boxStyle">

      <mx:VBox width="100%">
      //THIS DOES NOT WORK AND IS WHAT I'M TRYING TO MAKE WORK
      //<mx:Text width="{box.width-100}" id="question" htmlText = "{aryQuestion}"/>

      //THIS WORKS
      <mx:Text width="{box.width-100}" id="question" htmlText = "{feedRequest.lastResult.rss.channel.questions.question.getItemAt(0).title}"/>

      <mx:List
      id = "answers"
      dataProvider="{feedRequest.lastResult.rss.channel.questions.question.getItemAt(0).answers .answer}"
      change="answerSelected(event)"
      labelFunction="answerGet"/>
      </mx:VBox>
      </mx:Panel>

      </mx:Application>



      What I'm trying to do is out put the first question and its answers. On click of an answer, display the next question and answers, and so on.

      What am I doing wrong with the resultEvent? or anything else?

      thanks,

      Steve Odom
      http://www.trivionomy.com
      http://ww
        • 1. Re: Use of arrayCollection and resultEvent
          Flex harUI Adobe Employee
          XML is lots of fun.

          I usually put the XML inline to make sure it isn't a translation problem in the web service layer. Then I add trace statements at various points to dump out data types and nodes. I test for (obj is XML) and (obj is XMLList) and, most importantly, I use toXMLString() to print the node.

          It appears you are binding to an ArrayCollection which doesnt make sense to me. I would think you would need to bind to the aryQuestion.current or something more fine grained.
          • 2. Re: Use of arrayCollection and resultEvent
            djsodom Level 1
            Thanks. You correctly identified my problem, but I was copying an example from somewhere else (can't find it now) that presumably worked. In any case, here is my revised, working code:

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
            layout="absolute"
            creationComplete="initApp()">

            <mx:Script>
            <![CDATA[
            import flash.external.ExternalInterface;
            import mx.collections.ArrayCollection;
            import mx.rpc.events.*;

            [Bindable]
            public var qCollection:ArrayCollection
            private var qArray:Array;
            private var qCount:int;
            private var qPosition:int = 0;

            [Bindable]
            public var aCollection:ArrayCollection;
            private var aArray:Array;

            [Bindable]
            private var qText:String;

            private function initApp():void {
            ExternalInterface.call("alert", "correct");
            srv.send();
            qCollection = new ArrayCollection();
            aCollection = new ArrayCollection();
            }

            private function resultHandler(event:ResultEvent):void {
            qArray = event.result.rss.channel.list.question.source as Array;
            qCollection.source = qArray;
            qCount = qCollection.length;
            qText = qCollection[qPosition].text;
            aArray = qArray[qPosition].answer.source as Array
            aCollection.source = aArray;
            }

            private function answerGet(item:Object):String {
            if (item.text==undefined)
            {return null;}
            else
            {return item.text}
            }

            private function answerSelected(event:flash.events.Event):void {
            //is it correct?
            if (answers.selectedItem.correct == "y") {
            ExternalInterface.call("alert", "correct");
            //update correct count
            }
            //change question
            qPosition ++
            qText = qCollection[qPosition].text;
            aArray = qArray[qPosition].answer.source as Array
            aCollection.source = aArray;
            }
            ]]>
            </mx:Script>
            <mx:HTTPService id="srv" url="test.xml" useProxy="false" result="resultHandler(event)"/>

            <mx:Panel x="10" y="10" width="475" height="400" layout="absolute"
            title="Play. Trivia." id="box" styleName="boxStyle">

            <mx:VBox width="100%">
            <mx:Text width="{box.width-100}" id="question" htmlText = "{qText}"/>
            <mx:List id = "answers"
            dataProvider="{aCollection}"
            change="answerSelected(event)"
            labelFunction="answerGet"/>
            </mx:VBox>
            </mx:Panel>
            </mx:Application>