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!
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!
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()
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.
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()
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.
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();
}
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?
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();
}
North America
Europe, Middle East and Africa
Asia Pacific