Saving items in List Component using Shared Objects (flash AS3)

601 views Asked by At

I'm making a simple list app for ios where you can enter a string of text and add it as a list item to a List component. For test purposes I'm currently just using the click of a button to add items to the list component via AS.

I want to be able to save what I'm writing into the list and I've been trying to do it using the SharedObject.getLocal("mySharedObject"); method.

This is my current code (I'm working with AS3 in the .FLA file):

import fl.controls.List;
import fl.data.DataProvider;
import fl.events.ListEvent;

var saveListObject:SharedObject;
var Question_list: List = new List();
Question_list.setSize(320, 390);
Question_list.move(0, 0);

addChild(Question_list);

init(); 

function init(): void { 

saveListObject = SharedObject.getLocal("test"); 

btnSave.addEventListener(MouseEvent.CLICK, saveList); 

     if(saveListObject.data.savedList == null){ 
          trace("No saved list yet."); 
          saveListObject.data.savedList = Question_list.addItem; 
     } else {
          trace("Saved list found.");
          loadList();
     }
}

addTextBtn.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);

function fl_MouseClickHandler(event: MouseEvent): void {
    trace("Mouse clicked");
    Question_list.addItem({
        label: "New item"

    });
}

function saveList(e:MouseEvent):void{
     saveListObject.data.savedList = Question_list.dataProvider; 
     trace("List Saved!");
     saveListObject.flush(); 
     trace(saveListObject.size); 
}

function loadList():void{
    Question_list.removeAll();
      for (var p in saveListObject.data) {
    Question_list.addItem(saveListObject.data.savedList);
      }
     trace("List Loaded!");
    trace(Question_list.dataProvider);
}

On my timeline I have a addTextBtn and a btnSave. My list component is called Question_list. saveListObject is my SharedObject.

I manage to save and load successfully, but it seems like the data I'm loading isn't being read correctly by Question_list. I can add as many list items as I want and all I get when I open up the .swf again is one (1) blank list item. I suspect I'm saving all the list data into an array and I'm not able to load it back into different list items with labels. (I'm using the label to show the text inside Question_list)

I hope someone here can guide me the right way.

Thanks a lot!

2

There are 2 answers

0
dr_ill On

The problem is how you access SharedObject. you have:

saveListObject.data.savedList = value;

I would use:

saveListObject.data["savedList"] = value;

Hope that helps

0
CyanAngel On

Your issue is the listLoad() function.

function loadList():void{
    Question_list.removeAll();
      for (var p in saveListObject.data) {
    Question_list.addItem(saveListObject.data.savedList);
      }
     trace("List Loaded!");
    trace(Question_list.dataProvider);
}

Specifically Question_list.addItem(saveListObject.data.savedList); savedList is an object containing the whole saved list and Question_list.addItem() only adds one item. So your creating one new list entry that is a representation of all your data.

I would suggest altering your loop like so:

 for each(var p in saveListObject.data.savedList) {
        Question_list.addItem(p);
 }

This will pull each row of your savedList, one by one and add it to the on-screen list.