Android Question storing map in list

gjoisa

Active Member
Licensed User
Longtime User
I can store multiple maps in a list and save it to device . But I can't retrieve it .
B4X:
Dim iMap As Map
    Dim ilist As List
    Dim name,age As String
    ilist.Initialize
    iMap.Initialize
    ilist.Clear
    iMap.Clear
    name = txtName.Text
    age = txtAge.Text
    If File.Exists(File.DirInternal,"sgj.txt") = True Then
        ilist = File.ReadList(File.DirInternal,"sgj.txt")
        iMap.Put("Name",name)
        iMap.Put("Age",age)
        ilist.Add(iMap)
        File.Delete(File.DirInternal,"sgj.txt")
        File.WriteList(File.DirInternal,"sgj.txt",ilist)
        txtName.Text = ""
        txtAge.Text = ""
        xui.MsgboxAsync("Success","SGJoisa")
    Else
        iMap.Put("Name",name)
        iMap.Put("Age",age)
        ilist.Add(iMap)
        File.WriteList(File.DirInternal,"sgj.txt",ilist)
        txtName.Text = ""
        txtAge.Text = ""
        xui.MsgboxAsync("Success","SGJoisa")
    End If
    
    Private Sub btnRead_Click
    Dim iMap As Map
    Dim ilist As List
    Dim name,age As String
    ilist.Initialize
    iMap.Initialize
    ilist.Clear
    iMap.Clear
    ilist = File.ReadList(File.DirApp,"sgj.txt")
    iMap = ilist.Get(0)
    name = iMap.Get("Name")
    age = iMap.Get("Age")
    txtName.Text = name
    txtAge.Text = age
End Sub

How to read it ?
 

William Lancee

Well-Known Member
Licensed User
Longtime User
Or B4XSerializer in RandomAccessFile library
See another example by @agraham https://www.b4x.com/android/forum/threads/java-use-of-b4xserializator.131434/post-828221

B4X:
    Dim dir As String = File.DirInternal        'Or your dir
    Dim listOfMaps As List
    listOfMaps.Initialize
    listOfMaps.Add(CreateMap("Akey1": "value1", "Bkey1": "value4"))
    listOfMaps.Add(CreateMap("Akey2": "value2", "Bkey1": "value5"))
    listOfMaps.Add(CreateMap("Akey3": "value3", "Bkey1": "value6"))
    Log(listOfMaps.Size)
   
    Dim ser As B4XSerializator        'include RandomAccessFile library
    File.WriteBytes(dir, "myData.dat", ser.ConvertObjectToBytes(listOfMaps))
    '....
    Dim listOfMaps2 As List = ser.ConvertBytesToObject(File.ReadBytes(dir, "myData.dat"))
    Log(listOfMaps2.Size)        'Log: 3
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
As an aside, B4Xserializer is also very good for cloning complex structures.

B4X:
    Dim ListOfMapsClone As List = ser.ConvertBytesToObject(ser.ConvertObjectToBytes(listOfMaps))
    Log(ListOfMapsClone.Size)        'Log: 3
 
Upvote 0
Top