12 Replies Latest reply: Aug 2, 2012 6:46 PM by kglad RSS

    Entering/Saving Text Data with AS3

    Claunchster

      I'm creating a desktop application for the beginning of the school year and I need to be able to have people manually fill in data in text fields (input text fields), primarily just names - and when they open the application they need that data to still be there.  I also want them to be able to edit and delete the data that they enter, possibly even using some kind of "save" button, just spitballin'.  What would be the best/easiest way to accomplish this?  Any ideas?

      Thanks!

        • 1. Re: Entering/Saving Text Data with AS3
          kglad MVP

          use the SharedObject.getLocal() static method.  you don't have to worry about reading and writing files.

          • 2. Re: Entering/Saving Text Data with AS3
            dmeN Community Member

            You can use SharedObject as kglad suggested, or you can also use AIR's File/FileStream to write to say a csv file, which can be useful if you want to graph data, etc. It's very easy actually - here's a nice 10 line example:

            http://www.numbmedia.com/blog/?p=159

            • 3. Re: Entering/Saving Text Data with AS3
              Claunchster Community Member

              Thanks guys, this has been very helpful . . . so here is what I'm using now:

               

              import flash.filesystem.File;

              import flash.filesystem.FileMode;

              import flash.filesystem.FileStream;

               

               

              var mySaveNewDataArray:Array = new Array("txtName1","txtName2", "txtName3", "txtName4", "txtName5", "txtName6", "txtName7", "txtName8", "txtName9", "txtName10");

              var file:File = File.desktopDirectory.resolvePath( "CTIS_BD_data.csv" );

              var stream:FileStream = new FileStream();

               

               

              stream.open( file, FileMode.APPEND );

              stream.writeUTFBytes( "\n" + mySaveNewDataArray );

              stream.close();

              file = null;

              stream = null;

               

              I'm writing to the file perfectly everytime, but it's not reading the data in my .csv when I start the AIR app back up again.  What am I missing?

               

              Thanks!

              • 4. Re: Entering/Saving Text Data with AS3
                kglad MVP

                you're making this a lot more complicated for no apparent reason.  

                 

                if you need to create a human readable file, it makes sense to use the file class and in that case, and with the code you're now using you need to use readUTFBytes().  are you?

                 

                and, if you don't need to use a human readable file, use the sharedobject:

                 

                 

                var mySaveNewDataArray:Array = new Array("txtName1","txtName2", "txtName3", "txtName4", "txtName5", "txtName6", "txtName7", "txtName8", "txtName9", "txtName10");

                var so:SharedObject=SharedObject.getLocal("dataSO");

                if(so.data){
                for(var s:String in so.data){  // this reads the data
                trace(s,so.data[s]);
                }
                }
                so.data.dataA=mySaveNewDataArray.concat();  // this writes the data
                so.flush()
                • 5. Re: Entering/Saving Text Data with AS3
                  Claunchster Community Member

                  Hey kglad, thanks for your help . . . here's what I'm using with FileStream and I tried using readUTFbytes(), but my data is still not writing anything to my xml, and thus not having anything to read from upon app startup:

                   

                  //-------------------------------------------------

                  import flash.filesystem.File;

                  import flash.filesystem.FileMode;

                  import flash.filesystem.FileStream;

                   

                  var mySaveNewDataArray:Array = new Array("txtName1","txtName2", "txtName3", "txtName4", "txtName5", "txtName6", "txtName7", "txtName8", "txtName9", "txtName10");

                  var file:File = File.desktopDirectory.resolvePath( "CTIS_BD_data.xml" );

                  var stream:FileStream = new FileStream();

                  stream.open(file, FileMode.WRITE);

                   

                  stream.open( file, FileMode.APPEND );

                  var prefsXML:XML = XML(stream.readUTFBytes(stream.bytesAvailable));

                  var outputString:String = '<?xml version="1.0" encoding="utf-8"?>\n';

                  outputString += prefsXML += mySaveNewDataArray.toXMLString();

                  stream.writeUTFBytes(outputString);

                  stream.close();

                  file = null;

                  stream = null;

                  //-------------------------------------------------

                   

                  When I use the SharedObject.getLocal() method I still am not having any data reading back to my text fields when the app starts back up.  As soon as I close the app, my data dies with it.

                  • 6. Re: Entering/Saving Text Data with AS3
                    kglad MVP

                    if your textfield is your_tf, use:

                     

                    var mySaveNewDataArray:Array = new Array("txtName1","txtName2", "txtName3", "txtName4", "txtName5", "txtName6", "txtName7", "txtName8", "txtName9", "txtName10");

                    var so:SharedObject=SharedObject.getLocal("dataSO");

                     

                    var tfS:String = "";

                    if(so.data){

                    for(var s:String in so.data){  // this reads the data

                    tfS+=so.data[s].toString();

                    }

                    }

                    your_tf.text = tfS;

                    so.data.dataA=mySaveNewDataArray.concat();  // this writes the data

                    so.flush()

                    • 7. Re: Entering/Saving Text Data with AS3
                      Claunchster Community Member

                      Apparently I have something way wrong. My input tfs are named in the array; txtName1, txtName2, etc. My goal is to be able to have whoever opens the application to input names in the various input tfs and have the app remember the names when it is closed out and reopened again.  So far, when I first open my app, the input tfs are empty.  I fill them in with random keystrokes for testing purposed, close the program, open it back up again and I get the names of my input tfs (named in the array) showing up in one of my "txtName1" input tf.  I have 10 input tfs in the image below.  I know I'm bound to be missing something simple.

                      example.png

                      • 8. Re: Entering/Saving Text Data with AS3
                        kglad MVP

                        use:

                         

                        var tfA:Array = new Array(txtName1,txtName2, txtName3, txtName4, txtName5, txtName6, txtName7, txtName8, txtName9, txtName10);  // no quotes

                        var so:SharedObject=SharedObject.getLocal("dataSO");

                         

                        if(so.data){

                        for(var i:int=0;i<tfA.length;i++){

                        tfA[i].text=so.data["tf"_text"+i];

                        }

                        }

                         

                        // when your textfields are ready to have their text saved, call saveTextF:

                         

                        function saveTextF():void{

                        for(i=0;i<tfA.length;i++){

                        so.data["tf_text_"+i]=tfA[i].text;

                        }

                        so.flush();

                        }

                        • 9. Re: Entering/Saving Text Data with AS3
                          Claunchster Community Member

                          Aarrrgh . . . I have been getting this error throughout the afternoon:

                           

                          TypeError: Error #2007: Parameter text must be non-null.

                                    at flash.text::TextField/set text()

                                    at CTIS_fla::mcBus127B_1/frame1()[CTIS_fla.mcBus127B_1::frame1:6]

                           

                          I have stripped everything else out of my .fla to make sure it wasn't a coding issue with anything else I have going on, but nope, still getting this same error. So if I understand what's going on here, my .fla is trying to read my tf when there is nothing in it, and there's nothing in it because I'm waiting for someone to fill in the tfs with their own info after the app starts.  Am I getting this straight?

                          • 10. Re: Entering/Saving Text Data with AS3
                            kglad MVP

                            use:

                             

                             

                             

                            var tfA:Array = new Array(txtName1,txtName2, txtName3, txtName4, txtName5, txtName6, txtName7, txtName8, txtName9, txtName10);  // no quotes

                            var so:SharedObject=SharedObject.getLocal("dataSO");

                             

                            for (var i:int=0; i<tfA.length; i++) {

                                if (so.data["tf_text_" + i]) {

                                    tfA[i].text = so.data["tf_text_" + i];

                                }

                            }

                             

                            // when your textfields are ready to have their text saved, call saveTextF:

                             

                            function saveTextF():void{

                            for(i=0;i<tfA.length;i++){

                            so.data["tf_text_"+i]=tfA[i].text;

                            }

                            so.flush();

                            }

                            • 11. Re: Entering/Saving Text Data with AS3
                              Claunchster Community Member

                              Thanks again for the assist!

                              • 12. Re: Entering/Saving Text Data with AS3
                                kglad MVP

                                you're welcome.