Flex 读取文本文件(值对格式),放入DataGrid

 mytest.txt,内容如下:

                variables1=Kakera&variables2=Eigo&variables3=Keirago

FileVariables.mxml

  1. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal" verticalAlign="middle" backgroundColor="white" creationComplete="init()">  
  2.   
  3.     <mx:Script> 
  4.         <![CDATA[ 
  5.             import mx.collections.ArrayCollection; 
  6.   
  7.             [Bindable] 
  8.             private var VARIABLES_URL:String = "mytest.txt"; 
  9.     
  10.             [Bindable] 
  11.             private var paramColl:ArrayCollection; 
  12.   
  13.             private var urlReq:URLRequest; 
  14.             private var urlLdr:URLLoader; 
  15.   
  16.             private function init():void { 
  17.                 /* Initialize the two ArrayCollections objects with empty arrays. */ 
  18.                paramColl = new ArrayCollection(); 
  19.   
  20.                 /* Initialize the URLRequest object with the URL to the file of name/value pairs. */ 
  21.                 urlReq = new URLRequest(VARIABLES_URL); 
  22.   
  23.                 /* Initialize the URLLoader object, assign the various event listeners, and load the specified URLRequest object. */ 
  24.                 urlLdr = new URLLoader(); 
  25.                 urlLdr.addEventListener(Event.COMPLETE, doEvent); 
  26.                 urlLdr.addEventListener(Event.OPEN, doEvent); 
  27.                 urlLdr.addEventListener(HTTPStatusEvent.HTTP_STATUS, doEvent); 
  28.                 urlLdr.addEventListener(IOErrorEvent.IO_ERROR, doEvent); 
  29.                 urlLdr.addEventListener(ProgressEvent.PROGRESS, doEvent); 
  30.                 urlLdr.addEventListener(SecurityErrorEvent.SECURITY_ERROR, doEvent); 
  31.                 urlLdr.load(urlReq); 
  32.             } 
  33.   
  34.             private function doEvent(evt:Event):void {   
  35.                 switch (evt.type) { 
  36.                     case Event.COMPLETE: 
  37.                         /* If the load was successful, create a URLVariables object from the URLLoader.data property and populate the paramColl ArrayCollection object. */ 
  38.                         var ldr:URLLoader = evt.currentTarget as URLLoader; 
  39.                         var vars:URLVariables = new URLVariables(ldr.data); 
  40.                         var key:String; 
  41.   
  42.                         for (key in vars) { 
  43.                             paramColl.addItem({key:key, value:vars[key]}); 
  44.                         } 
  45.                         params.visible = true;
  46.                         break; 
  47.                 } 
  48.             }  
  49.         ]]> 
  50.     </mx:Script>  
  51.   
  52.     <mx:VBox> 
  53.         <mx:Label text="Parameters:" /> 
  54.         <mx:DataGrid id="params" dataProvider="{paramColl}" rowCount="5" visible="false"> 
  55.             <mx:columns> 
  56.                 <mx:DataGridColumn dataField="key" headerText="Key" /> 
  57.                 <mx:DataGridColumn dataField="value" headerText="Value" /> 
  58.             </mx:columns> 
  59.         </mx:DataGrid> 
  60.     </mx:VBox>  
  61.   
  62. </mx:Application>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值