Android Question Error when trying to get a List from a Map

Melghost

Member
Licensed User
Hi!

What's the right way to do this?

B4X:
Sub Globals
    Dim List1 As List
    Dim Map1 As Map
End Sub

Sub Activity_Create(FirstTime As Boolean)
    List1.Initialize
    List1.Add("Hello!")

    Map1.Initialize
    Map1.Put("Lista",List1)

    File.WriteMap(File.DirRootExternal,"PruebaListMap.txt",Map1)

End Sub

Sub Button1_Click
    Dim List2 As List
    Dim Map2 As Map

    Map2.Initialize
    Map2=File.ReadMap(File.DirRootExternal,"PruebaListMap.txt")

    List2.Initialize
    List2=Map2.Get("Lista")            'Error: String cannot be cast to java.util.List

End Sub
Thanks
 

DonManfred

Expert
Licensed User
Use KeyValueStore instead.

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private kvs As KeyValueStore
End Sub
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")
    If FirstTime Then
        kvs.Initialize(File.DirDefaultExternal, "datastore")
    End If

    Dim List1 As List
  Dim Map1 As Map
    List1.Initialize
  List1.Add("Hello!")
    List1.Add("World")
  Map1.Initialize
  Map1.Put("Lista",List1)
   
    'File.WriteMap(File.DirRootExternal,"PruebaListMap.txt",Map1)
    kvs.PutObject("Lista",Map1)

End Sub

Sub Button1_Click
    Dim List2 As List
    Dim Map2 As Map
    Map2.Initialize
  Map2=kvs.GetObject("Lista")
  Log(Map2)
    List2.Initialize
  List2=Map2.Get("Lista")            'Error: String cannot be cast to java.util.List
    Log(List2.Size)
    For i = 0 To List2.Size-1
        Log(List2.Get(i))
    Next

End Sub
 

Attachments

DonManfred

Expert
Licensed User
File.WriteMap(File.DirRootExternal,"PruebaListMap.txt",Map1)
File.writemap writes each entry in the map into one line.
File.readmap will read such a map... The result is that the list is no more a list. Its now ONE string

Using KVS you can store (and read) objects like the one you created...
 

Melghost

Member
Licensed User
Hi again!
How do I have to initialize the KeyValueStore object to access data from different files?

I want to store a lot of recipes in different folders, and the user will be able to add new folders and recipes. I would like to store every recipe in its own file. So I think I would have to initialize the object every time the user selects a recipe, instead of doing it in Activity_Create. Am I right? I'm trying to do it this way but I'm not sure it's the best way. Is there a better alternative?

Thank you.
 

DonManfred

Expert
Licensed User
you can reuse you kvs. Just call Initialize again with the new filename you want to use.
Please note that you need to use kvs.Close as it closes the used sqlite-database which is used by KVS to store the content
B4X:
    If kvs.IsInitialized Then
        kvs.Close
        kvs.Initialize(File.DirDefaultExternal, newfilename)
    End If
 
Top