How do I save and load a list of objects?

andymc

Well-Known Member
Licensed User
Longtime User
I have a list of objects (each object contains a string and 3 ints), I can save the list to a file using file.writelist but I can't load them again, as loadlist just loads strings. Do I have to save all my objects as seperate strings and then reload them back into objects in the list again?
 

andymc

Well-Known Member
Licensed User
Longtime User
Wow! The randomaccessfile library is brilliant!!! I thought I'd have to deconstruct my list of objects into seperate strings, then save it all off, then ty to load them all back into objects using some overly complex function. This works great!:sign0188::sign0188::sign0188::sign0188:
 
Upvote 0

andymc

Well-Known Member
Licensed User
Longtime User
Sorry! Yes, good point.

Tick RandomAccessFile under the libs tab in the IDE

then the code:
Loading the list from file
B4X:
Sub Globals
   Dim datesList As List
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("Dates")

   'initialize dates list
   datesList.Initialize

   'Read important dates list from data file
   Dim DatesFile As RandomAccessFile
   DatesFile.Initialize(File.DirInternal,"importantDates.dat",False)
   datesList = DatesFile.ReadObject(0)
   DatesFile.Close

Saving the list to file:
B4X:
'save important dates list to text file
   Dim DatesFile As RandomAccessFile
   DatesFile.Initialize(File.DirInternal,"importantDates.dat",False)
   DatesFile.WriteObject(datesList,True,0)
   DatesFile.Close
 
Upvote 0

Adrian Jansen

Member
Licensed User
Longtime User
Works like a charm on lists, after spending hours struggling with the stringutils load and save, where I had a list composed of an array of an array of strings.
 
Upvote 0
Top