2012-07-25 105 views
0

我正在爲android製作一個簡單的程序,什麼可以添加,讀取和寫入文本行到一個txt文件並顯示在列表中。 我得到了saveFile();函數將todo_items數組寫入文件todo.txt。 但由於某種原因,而不是顯示列表中的真實文本行,它給了我數字... 例如,當我添加「喂狗,喂貓,餵魚,喂鳥」,然後刪除結果中的3d項它給了我「0,1,2」。不知道爲什麼。下面是函數的代碼如何將數組寫入文件

<?xml version="1.0" encoding="utf-8"?> 
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
      xmlns:s="library://ns.adobe.com/flex/spark" 
      height="494" creationComplete="readFile()" 
      > 
<fx:Script> 
    <![CDATA[ 
     public var intTemp:int; 
     import mx.collections.ArrayCollection; 
     [Bindable] public var todo_items:ArrayCollection; 
     private function readFile():void 
     { 
      var todoFile:File =File.applicationStorageDirectory.resolvePath("todo.txt"); 


       var fs:FileStream = new FileStream(); 
       fs.open(todoFile, FileMode.READ); 
       var result:String = fs.readUTFBytes(fs.bytesAvailable); 
       var items:Array = result.split("\n"); 
       items.pop(); 
       todo_items = new ArrayCollection(items); 
       fs.close(); 

     } 
     private function writeFile():void 
     { 
      var todoFile:File = File.applicationStorageDirectory.resolvePath("todo.txt"); 


      var fs:FileStream = new FileStream(); 
      fs.open(todoFile, FileMode.APPEND); 


      fs.writeUTFBytes(task_txt.text + "\n"); 


      fs.close(); 
      readFile(); 
     } 

     private function deleteItem():void 
     {   
       var todoFile:File =File.applicationStorageDirectory.resolvePath("todo.txt"); 
       var fs:FileStream = new FileStream(); 
       fs.open(todoFile, FileMode.READ); 
       var result:String = fs.readUTFBytes(fs.bytesAvailable); 
       var items:Array = result.split("\n"); 
       items.pop(); 
       todo_items = new ArrayCollection(items); 
       todo_items.removeItemAt(intTemp); 
       fs.close(); 
       safeFile(); 
     } 
     private function safeFile():void 
     { 

      var todoFile:File = File.applicationStorageDirectory.resolvePath("todo.txt"); 


      var fs:FileStream = new FileStream(); 
      fs.open(todoFile, FileMode.WRITE); 

      for(var item:String in todo_items) 
      { 
       fs.writeUTFBytes(item + "\n") 
      } 

      fs.close(); 
      readFile(); 



     } 

    ]]> 
</fx:Script> 


<s:List id="todo_list" left="10" right="10" top="142" bottom="87" dataProvider="{todo_items}"/> 
<s:Button left="11" right="10" top="69" height="65" label="Save task" click="writeFile(); task_txt.text = null" 
      enabled="{task_txt.text.length > 0}"/> 
<s:TextInput id="task_txt" left="10" right="10" top="10" height="51" prompt="Specify a task"/> 
<s:Button left="10" right="10" bottom="14" label="Delete" 
      click="intTemp = todo_list.selectedIndex; todo_items.removeItemAt(todo_list.selectedIndex); deleteItem()" 
      enabled="{todo_list.selectedIndex != -1}"/> 


</s:Application> 
+0

'todo_items'從哪裏來?請給更多的代碼。或至少是其聲明 – poussma 2012-07-25 09:33:32

+0

以下是完整的代碼 – 2012-07-25 09:37:46

+0

代碼在哪裏? – poussma 2012-07-25 09:40:45

回答

0

好,for(var item in array)item到所有鍵的數組中,即0,1,2,...

爲了得到實際的項目,你需要執行以下操作之一:

for(var item:String in todo_items) 
{ 
    fs.writeUTFBytes(todo_items[item] + "\n") 
} 

for each(var item:String in todo_items) 
{ 
    fs.writeUTFBytes(item + "\n") 
} 

我RECO修正第二個因爲它更清潔。

+0

謝謝收穫!剛剛嘗試過 - 仍然是一樣的。 – 2012-07-25 09:48:57

+0

或者也許它的Flash Builder竊聽,YEESSS它的工作!非常感謝! – 2012-07-25 09:56:58