Android Question trouble using map, where is my error ?

marcick

Well-Known Member
Licensed User
Longtime User
turning crazy ...
look at this code. WIthout using writemap and readmap everything works, otherwise I get an error

B4X:
Dim map1 As Map
    Dim list1 As List
    map1.Initialize
    list1.Initialize
   
    list1.Add("hello")
    list1.Add(123)
    map1.Put("key1",list1)
   
    list1.clear
    list1.Add("hello")
    list1.Add(456)
    map1.Put("key2",list1)
   
    'File.WriteMap(File.DirInternal,"test",map1)        ' uncomment these two lines and you get an error below
    'map1=File.ReadMap(File.DirInternal,"test")            '
   
    For j=0 To map1.Size-1
        list1=map1.GetValueAt(j)                        ' java.lang.ClassCastException: java.lang.String cannot be cast to java.util.List
        Log (list1)
    Next
 

marcick

Well-Known Member
Licensed User
Longtime User
uhmm ... why without writemap and readmap it works ?
For each key in the map, I want to retrieve the couple of values that has been stored as list ("hallo", 123 and "hallo", 456)
I do it with:

B4X:
For j=0 To map1.Size-1
        list1=map1.GetValueAt(j)                     
        Log (list1.get(0))
        Log (list1.get(1))
Next

It works unless I use writemap and readmap to store and retrieve the data
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Because you didnt read the documentation. Here I provide for you:

WriteMap (Dir As String, FileName As String, Map As Map)
Creates a new file and writes the given map. Each key value pair is written as a single line.
All values are converted to strings.
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
... so I have to retrieve the values as a single string and then split it
Thanks
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
but I want to use the map to manage these data, because it's easy then to scan throught keys and value ....

the string that I read (that was a list when I put it in the map) is

[45.45540763167773, 8.313687890768051, pushpin48.png, MyPushpin 67928, com.google.android.gms.maps.model.Marker@41f80d00]

Now I have to split it and remove also the blanks and the square bracket at the beginning and end
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…