经过多次试验后第一个成功地实现 HTTPService 与 MXML 之间传递数据,ArrayCollection 与DataGrid 之间成功绑定

xml数据:

Xml代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <shuju>  
  3.     <shuiguo month="Jan-04">  
  4.         <apple>81768</apple>  
  5.         <orange>60310</orange>  
  6.         <banana>43357</banana>  
  7.     </shuiguo>  
  8.     <shuiguo month="Feb-04">  
  9.         <apple>81156</apple>  
  10.         <orange>58883</orange>  
  11.         <banana>49280</banana>  
  12.     </shuiguo>  
  13. </shuju>  
<?xml version="1.0" encoding="UTF-8"?>

<shuju>

    <shuiguo month="Jan-04">

        <apple>81768</apple>

        <orange>60310</orange>

        <banana>43357</banana>

    </shuiguo>

    <shuiguo month="Feb-04">

        <apple>81156</apple>

        <orange>58883</orange>

        <banana>49280</banana>

    </shuiguo>

</shuju>

 程序代码:

Xml代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="http.send()">  
  3.     <mx:Script>  
  4.         <![CDATA[  
  5.             import mx.rpc.events.ResultEvent;  
  6.             import mx.collections.ArrayCollection;  
  7.             [Bindable]  
  8.             public var myData:ArrayCollection;  
  9.               
  10.             public function resultHandler(event:ResultEvent):void{  
  11.                         error.text = event.result as String;  
  12.                         myData = new ArrayCollection(event.result.shuju.shuiguo);             
  13.                 }  
  14.         ]]>  
  15.     </mx:Script>  
  16.     <!--<mx:HTTPService id="http" url="http://localhost:8080/XML2ArrayCollection/data/data.xml" useProxy="false" result="resultHandler(event)"/>-->  
  17.     <mx:HTTPService id="http" url="http://localhost:8080/XML2ArrayCollection/data/data.xml" useProxy="false" result="myData=ArrayCollection(http.lastResult.shuju.shuiguo)"/>  
  18.       <mx:Panel title="Line Chart" width="465" height="496">  
  19.      <mx:LineChart id="chart" dataProvider="{myData}">  
  20.         <mx:horizontalAxis>  
  21.            <mx:CategoryAxis categoryField="month"/>  
  22.         </mx:horizontalAxis>  
  23.         <mx:series>  
  24.            <mx:LineSeries yField="apple" name="Apple"/>  
  25.            <mx:LineSeries yField="orange" name="Orange"/>  
  26.            <mx:LineSeries yField="banana" name="Banana"/>  
  27.         </mx:series>  
  28.      </mx:LineChart>  
  29.      <mx:TextArea width="393" id="error"/>  
  30.   </mx:Panel>  
  31. </mx:Application>  
<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="http.send()">

	<mx:Script>

		<![CDATA[

			import mx.rpc.events.ResultEvent;

			import mx.collections.ArrayCollection;

			[Bindable]

			public var myData:ArrayCollection;

			

			public function resultHandler(event:ResultEvent):void{

				        error.text = event.result as String;

	            		myData = new ArrayCollection(event.result.shuju.shuiguo);			

				}

		]]>

	</mx:Script>

	<!--<mx:HTTPService id="http" url="http://localhost:8080/XML2ArrayCollection/data/data.xml" useProxy="false" result="resultHandler(event)"/>-->

	<mx:HTTPService id="http" url="http://localhost:8080/XML2ArrayCollection/data/data.xml" useProxy="false" result="myData=ArrayCollection(http.lastResult.shuju.shuiguo)"/>

	  <mx:Panel title="Line Chart" width="465" height="496">

     <mx:LineChart id="chart" dataProvider="{myData}">

        <mx:horizontalAxis>

           <mx:CategoryAxis categoryField="month"/>

        </mx:horizontalAxis>

        <mx:series>

           <mx:LineSeries yField="apple" name="Apple"/>

           <mx:LineSeries yField="orange" name="Orange"/>

           <mx:LineSeries yField="banana" name="Banana"/>

        </mx:series>

     </mx:LineChart>

     <mx:TextArea width="393" id="error"/>

  </mx:Panel>

</mx:Application>

 现在有一个问题我不明白:

我把那个httpservice改写成:

Xml代码
  1. <mx:HTTPService id="http" url="http://localhost:8080/XML2ArrayCollection/data/data.xml" useProxy="false" result="resultHandler(event)"/>  
<mx:HTTPService id="http" url="http://localhost:8080/XML2ArrayCollection/data/data.xml" useProxy="false" result="resultHandler(event)"/>

 然后添加了脚本:

Js代码
  1. public function resultHandler(event:ResultEvent):void{   
  2.             error.text = event.result as String;   
  3.                 myData = new ArrayCollection(event.result.shuju.shuiguo);              
  4.     }  
			public function resultHandler(event:ResultEvent):void{

				        error.text = event.result as String;

	            		myData = new ArrayCollection(event.result.shuju.shuiguo);			

				}

 这样就不行了,我测试了下,好像event.result里面没有内容,没有返回xml内容吗?谁大牛帮忙解释下。

 

Reference:

1. http://livedocs.adobe.com/flex/201/html/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Book_Parts&file=charts_intro_108_12.html

 

2. http://hillelcoren.com/2008/09/21/flex-tip-convert-xml-to-arraycollection/

3. http://dreamweaverforum.info/flex/20292-converting-xml-file-array-collection.html

你可能感兴趣的:(arrayCollection)