2 Replies Latest reply on Feb 22, 2011 12:06 PM by Shongrunden

    Issue Putting Data from ArrayCollection into List

    Fran711

      I'm having trouble getting data that has been pulled from a textArea and put into an ArrayCollection to be displayed in a List.  The data is being saved and then read as a file from local storage.  When I run the application, nothing appears in the created list even though I get no compile-time errors.

       

      Here is my List code:

       

       

      <fx:Script>
       <![CDATA[
       import dh.DataHandling;
       import mx.collections.ArrayCollection;
       import mx.collections.ArrayList;
       import mx.events.FlexEvent;
       import spark.effects.SlideViewTransition;
       
       [Bindable]
       private var dataR:DataHandling = new DataHandling();
       
       protected function button1_clickHandler(event:MouseEvent):void  // pushed home button
       {
       var svt:SlideViewTransition = new SlideViewTransition(300, SlideViewTransition.SLIDE_RIGHT);
       navigator.pushView(views.HomePage, event.relatedObject, svt);
       }
       
       ]]>
       </fx:Script>
       
       
       <fx:Declarations>
       <!-- Place non-visual elements (e.g., services, value objects) here -->
       </fx:Declarations>
       <s:actionContent>
       <s:Button label="Delete"/>
       <s:Button label="Home" click="button1_clickHandler(event)" skinClass="skins.homeButtonSkin"/>
       </s:actionContent>
       
       <s:Image x="0" y="-80" height="603" source="mainapp1.jpg"/>
       
       <s:List  id="myCardsList" x="10" y="10" width="1004" height="500" top="0" bottom="0" left="0" right="0"
        dataProvider="{dataR.cards as ArrayCollection}">
       <s:itemRenderer>
       <fx:Component>
       <s:MobileIconItemRenderer label="{data.textTitle}"/>
       </fx:Component>
       </s:itemRenderer>
       </s:List>
      
      

       

       

      Here's the "saving/reading the file" class used in creating the ArrayCollection:  ( DataHandling.as )

       

       

      package dh
      {
       public class DataHandling
       {
       import flash.events.IOErrorEvent;
       import flash.filesystem.File;
       import flash.filesystem.FileMode;
       import flash.filesystem.FileStream;
       
       import mx.collections.ArrayCollection;
       
       [Bindable]
       public var cards:ArrayCollection = new ArrayCollection();
       public var file:File;
       public var fileStream:FileStream;
       public var fileName:String = "Initial String";
       public var directory:String = "SimpleSaveFromAIR";
       public var textTitle:String;
       
       
       public function DataHandling()
       {
       }
       public function add():void
       {
       // new object
       var card:Object = new Object();
       card.tcard = textTitle;
       //card.mtext1 = mtext1.text;
       ///card.mtext2 = mtext2.text;
       cards.addItem(card);
       }
       
       public function save():void
       {
       // create file object (resolve user's directory, add own directory)
       file = File.documentsDirectory.resolvePath(directory + "\\" + fileName);
       // FileStream for writing the file
       fileStream = new FileStream();
       // open the file in write mode
       fileStream.open(file, FileMode.WRITE);
       // Write the ArrayCollection object of cards to the file
       fileStream.writeObject(cards);
       // close stream
       fileStream.close();
       }
       
       public function read():void
       {
       // get correct path
       file = File.documentsDirectory.resolvePath(directory + "\\" + fileName);
       
       // read if exists
       if(file.exists)
       {
       // FileStream for reading the file
       fileStream = new FileStream();
       // open the file in read mode
       fileStream.open(file, FileMode.READ);
       // Read the ArrayCollection object of cards from the file
       cards = fileStream.readObject() as ArrayCollection;
       //close Stream
       fileStream.close();
       }
       else
       {
       // some sample data + save if file does not exist
       cards = new ArrayCollection();
       var card:Object = new Object();
       card.tcard = "Sample Title";
       card.mtext1 = "Sample Text";
       
       cards.addItem(card);
       save();
       }
       }
       }
      }