• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Entering/Saving Text Data with AS3

Community Beginner ,
Jul 29, 2012 Jul 29, 2012

Copy link to clipboard

Copied

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!

TOPICS
ActionScript

Views

8.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 29, 2012 Jul 29, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 30, 2012 Jul 30, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 30, 2012 Jul 30, 2012

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 30, 2012 Jul 30, 2012

Copy link to clipboard

Copied

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);
}
}
so.data.dataA=mySaveNewDataArray.concat();  // this writes the data
so.flush()

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 30, 2012 Jul 30, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 30, 2012 Jul 30, 2012

Copy link to clipboard

Copied

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.toString();

}

}

your_tf.text = tfS;

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

so.flush()

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 31, 2012 Jul 31, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 01, 2012 Aug 01, 2012

Copy link to clipboard

Copied

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.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.text;

}

so.flush();

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 01, 2012 Aug 01, 2012

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 01, 2012 Aug 01, 2012

Copy link to clipboard

Copied

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.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.text;

}

so.flush();

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 02, 2012 Aug 02, 2012

Copy link to clipboard

Copied

Thanks again for the assist!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 02, 2012 Aug 02, 2012

Copy link to clipboard

Copied

LATEST

you're welcome.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines