Android Question WriteList and ReadList

sirjo66

Well-Known Member
Licensed User
Longtime User
Hello to all,
I'm trying to Read and Write a List of Maps, but it don't works :(

Here is the code:
B4X:
   Dim Ll As List
   Ll.Initialize
   
   Dim Mm As Map
   Mm.Initialize
   Mm.Put("Sigla", "AA")
   Mm.Put("Provincia", "Prima provincia")
   Mm.Put("Ipt", "30%")
   Mm.Put("Perc", "30")
   Ll.Add(Mm)
   
   Dim Mm As Map
   Mm.Initialize
   Mm.Put("Sigla", "BB")
   Mm.Put("Provincia", "Seconda provincia")
   Mm.Put("Ipt", "20%")
   Mm.Put("Perc", "20")
   Ll.Add(Mm)

   Dim Mm As Map
   Mm.Initialize
   Mm.Put("Sigla", "CC")
   Mm.Put("Provincia", "Terza provincia")
   Mm.Put("Ipt", "10%")
   Mm.Put("Perc", "10")
   Ll.Add(Mm)

   File.WriteList(File.DirDefaultExternal, "prov.cfg", Ll)

   ' now I read the List   
   Ll = File.ReadList(File.DirDefaultExternal , "prov.cfg")
   
   For x = 0 To Ll.Size - 1
     Dim Mm As Map
     Mm = Ll.Get(x)   ' <<<<<<<<< this don't work
   Next
 

DonManfred

Expert
Licensed User
Longtime User
File.Writelist will write each entry of the list as String to the file. NOTE that the map now is a string.

Use RandomAccessfile.WriteB4XObject to write such a list of maps.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
but it don't works
it works!

Read the documentation carefully.

File. WriteList (Dir As String, FileName As String, List As List)
Writes each item in the list as a single line.
Note that a value containing CRLF will be saved as two lines (which will return two item when read with ReadList).
All values will be converted to strings.
Example:
File.WriteList (File.DirInternal, "mylist.txt", List1)
 
Upvote 0

sirjo66

Well-Known Member
Licensed User
Longtime User
Many thanks to KMatle and DonManfred

Yes, the error is that "I can't convert a String to a Map", and I'm sorry but I haven't read very well the documentation :(

I'll try with RandomAccessfile.WriteB4XObject

Many thanks
Sergio
 
Upvote 0
Top