Android Question Retrieving Type Data From File.ReadMap Gives Imcompatible Types Error

Mahares

Expert
Licensed User
Longtime User
When I extract the map data from the saved map file as shown below, I get the error on line 62 as shown below: But if I put the Type members data in a map before saving it , it works well. How would you read the data from the saved map and make it display the 'Type' members. Thank you
B4X:
Type StudentType(id As Int, name As String, myclass As String, section As String)
B4X:
Sub DisplayData
    Dim MyMap As Map
    MyMap.Initialize
    MyMap=File.Readmap(File.DirInternal, "maptrivia")    
    For Each s As StudentType In MyMap    'line 62  error
        s = MyMap.GetValueAt(0)
        Log($"${s.id} ${s.name}  ${s.myclass}  ${s.section}"$)  
    Next
End Sub

B4X:
B4A line: 62
For Each s As StudentType In MyMap
src\b4a\example\main.java:491: error: incompatible types: Map cannot be converted to IterableList
final anywheresoftware.b4a.BA.IterableList group4 = _mymap;
 

drgottjr

Expert
Licensed User
Longtime User
as the error says, map is not iterable. step through your map:
B4X:
For i = 0 to Map.Size - 1
 '   Log("Key: " & Map.GetKeyAt(i))
  '  Log("Value: " & Map.GetValueAt(i))
   dim s as studenttype = map.getValueat(i)
Next
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
as the error says, map is not iterable. step through your map:
I stepped thru it and got similar results. I think it has to do with the data extracted as a string when read from a saved map file. Can you try it and let us know.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There are several other mistakes that you should avoid:
B4X:
 Dim MyMap As Map
    MyMap.Initialize
    MyMap=File.Readmap(File.DirInternal, "maptrivia")
Don't initialize the object if you are assigning a different object to the same variable.
Correct:
B4X:
Dim MyMap As Map = File.ReadMap(...)

Never use GetValueAt or GetKeyAt.
Iterating over a map:
B4X:
For Each Key As Object In MyMap.Keys 'change Object to correct type if type is known
  Dim Value As StudentType = MyMap.Get(Key)
Next
'you can also iterate over the values:
[code]
For Each Student As StudentType In MyMap.Values

Next

As you wrote, File.WriteMap / ReadMap is only good for simple values as it creates a text file. You should either use B4XSerializator and save the data with File.WriteBytes or just choose the best option which is to use KeyValueStore2.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
MyMap=File.Readmap(File.DirInternal, "maptrivia")
File. ReadMap (Dir As String, FileName As String) As Map

Reads the file and parses each line as a key-value pair (of strings).

You should use b4xserialisator to store and read a list with customtypes

Edit: too late ;-)
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
terating over a map:
I tied both variances and neither worked:
B4X:
Sub DisplayData
    Dim MyMap As Map
    MyMap=File.Readmap(File.DirInternal, "maptrivia")
    For Each Key As Int In MyMap.Keys 'change Object to correct type if type is known
       Dim s As StudentType = MyMap.Get(Key)    'error
       Log($"${s.id} ${s.name}  ${s.myclass}  ${s.section}"$)  
    Next
    'java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
End Sub

B4X:
Sub DisplayData
    Dim MyMap As Map
    MyMap=File.Readmap(File.DirInternal, "maptrivia")      
    For Each s As StudentType In MyMap.Values
        Log($"${s.id} ${s.name}  ${s.myclass}  ${s.section}"$)  'error
    Next
'    java.lang.RuntimeException: Field: id not found in: java.lang.String
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I am not familiar with b4xserialisator
Make your live more easy. Use KVS2.

Store the map to a key of your needs.
Read back the map from KVS2 using the Key used. Iterate through your Map

Or maybe better to use a List of customTypes and store the list.
It depends on your needs. Map, List, both are possible in KVS2. Even combined ones.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…