Android Question How to Save a B4XOrderedMap to a File and Read from it

Mahares

Expert
Licensed User
Longtime User
B4X:
Private mp As B4XOrderedMap
    mp.Initialize
    mp.Put("1","test1")
File.Writemap and File Readmap do not exist for an ordered map, yet. I have not had to use this new type of map, but I am curious how.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Have you tried to store/load it to/from a KVS2?
If it works then you need to use b4xSerializator i guess
Both will work.

B4X:
Dim ser As B4XSerializator
File.WriteBytes(File.DirInternal, "map.dat", ser.ConvertObjectToBytes(YourMap))
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Luca is right

It will not work. B4XOrderedMap is a class and class instances cannot be serialized like this. I was confused as the custom map is made of a List and Map which are supported.
If there is a need I will add two new subs that will make it possible to read and write it with B4XSerializator.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
It will not work. B4XOrderedMap is a class a
Thanks Erel. I was just going to post my test that it did not work either.
B4X:
mp.Initialize
    mp.Put("1","test1")
    mp.Put("5","test")
    mp.Put("2","test2")
    mp.Keys.Sort(True)
    Dim ser As B4XSerializator
    File.WriteBytes(File.DirInternal, "map.dat", ser.ConvertObjectToBytes(mp))
B4X:
Error occurred on line: 34 (Main)
java.lang.RuntimeException: Cannot serialize object: [b4xcollections=null, list=(ArrayList) [1, 2, 5], main=null
, map=(MyMap) {1=test1, 5=test, 2=test2}, starter=null]
                at anywheresoftware.b4a.randomaccessfile.B4XSerializator.writeType(B4XSerializator.java:258)
                at anywheresoftware.b4a.randomaccessfile.B4XSerializator.writeObject(B4XSerializator.java:224)
Hey, Manfred: Erel is also human, he seldom makes mistakes, but we have to question him every once in a while. Otherwise the forum becomes a cult. Do you think Erel's products would have reached these heights without the support and contributions from his 100000 members, including a few like, @DonManfred , @klaus , @LucaMs and the list goes on and on.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…