I'm having a hard time trying to get my array to save correctly into a textfile. The text file is formatted in a one-entry-per-line fashion thus I use this:
var newFavString:String = arFavourites.join("\r\n");
favStream.open(favFile, FileMode.WRITE);
favStream.writeUTFBytes(newFavString);
favStream.close();
But somewhere, somehow it's adding blank lines which may be caused by spaces within some of the array entries but that shouldn't affect the joining process I don't think.
I used this to clean up the arrays just in case and it still shows some undefined objects in the trace:
for (i=0; i<arFavourites.length-1; i++) {
if (arFavourites[i] == undefined) {
arFavourites.splice(i,1);
}
trace(arFavourites[i]);
}
And this is how I am importing the data into the array:
var favFile:File = File.userDirectory;
favFile = favFile.resolvePath("favourites.txt");
var favStream:FileStream = new FileStream();
var arFavourites = new Array();
var FavCount:int = 0;
favStream.open(favFile, FileMode.READ);
var fileContents:String = favStream.readUTFBytes(favStream.bytesAvailable);
arFavourites = fileContents.split("\r\n");
for (i=0; i<arFavourites.length-1; i++) {
if (arFavourites[i] == undefined) {
arFavourites.splice(i,1);
}
}
favStream.close();
When I remove something, I search for it in the arrays and splice it out. Maybe the error is there.
var k:int;
var i:int;
trace("Selected Index: " + dg.selectedIndex);
if (dg.length == 0) {
return;
}
for (k=0; k<dg.length; k++) {
if (dg.isItemSelected(dg.getItemAt(k))) {
for (i=0; i<arFavourites.length; i++) {
if (arFavourites[i] == dg.getItemAt(k)[1]) {
arFavourites.splice(i,1);
}
if (arFavourites[i] == dg.getItemAt(k)[2]) {
arFavourites.splice(i,1);
}
if (arFavourites[i] == dg.getItemAt(k)[3]) {
arFavourites.splice(i,1);
}
if (arFavourites[i] == dg.getItemAt(k)[4]) {
arFavourites.splice(i,1);
}
if (arFavourites[i] == dg.getItemAt(k)[5]) {
arFavourites.splice(i,1);
}
}
dg.removeItemAt(k);
dg.scrollToIndex(k);
dg.clearSelection();
break;
}
}
for (i=0; i<arFavourites.length-1; i++) {
if (arFavourites[i] == undefined) {
arFavourites.splice(i,1);
}
}
var newFavString:String = arFavourites.join("\r\n");
trace(newFavString);
favStream.open(favFile, FileMode.WRITE);
favStream.writeUTFBytes(newFavString);
favStream.close();
When something is added or removed from the array, the text file is updated. I'm sure its something really simple and I would greatly appreciate a speedy response =) Thanks
Ok so I finally figured out a working solution.
By getting the selectedIndex
I could figure out which array element matched the dataGrid line and then splice it out.
Selected Index:
arFavourites.splice(dg.selectedIndex*5,5);
Remove row from DataGrid:
for (k=0; k<dg.length; k++) {
trace(dg.length);
if (dg.isItemSelected(dg.getItemAt(k))) {
dg.removeItemAt(k);
dg.scrollToIndex(k);
dg.clearSelection();
}}
ReWrite text file:
var newFavString:String = arFavourites.join("\r\n");
trace(newFavString);
favStream.open(favFile, FileMode.WRITE);
favStream.writeUTFBytes(newFavString);
favStream.close();