ReadMap2 usage question

mistermentality

Active Member
Licensed User
Longtime User
How can I read map contents in order?

I have an app which saves data in several maps then needs to read that map data in later from saved map files, but so the map keys and values are in the same order as they were when saved.

So as I understand it I can used ReadMap2 to read in a maps data and populate a map if that map is already populated but I seem to not be doing it right and was hoping if I put a snippet of the code I am using to read the maps someone more experienced can verify my code is correct.

B4X:
tempmap = File.ReadMap(File.DirInternal,"questionmap.map")
tempnum = tempmap.Size-1 ' tempnum now holds the size of questionmap, ie how many items are stored in it

For lp = 0 To tempnum
questionmap.Put("Item #" &lp,"")
Next
questionmap = File.ReadMap2(File.DirInternal,"questionmap.map",questionmap)
'questionmap now contains the contents of saved questionmap in correct order

So I create an empty map, called tempmap, find the size of the stored map by reading it into the temporary one and then I fill the permanent map (here it is called questionmap) with exactly the same amount of keys and values as the stored map I want to read into it - following the readmap2 example of key1 = "Item #1" value = "" for each key.

Then I use the ReadMap2 line to read the whole map in, in the order it was saved previously, however the map is not always in the same order. Have I misunderstood the use of ReadMap2?

Thanks,

Dave

EDIT: Please read my most recent reply and if you can help explain why the ReadMap2 command is not putting the map data in order I would really appreciate it as I have a number of maps in an app that I need to read in but keep them in order and unless I have misunderstood the command my code should work but it doesn't.
 
Last edited:

mistermentality

Active Member
Licensed User
Longtime User
Are you sure that questionmap is empty before you start?

Note that you can use KeyValueStore class to save the map. It will be simpler.

Thanks for quick answer.

Yes, I make sure map is empty first by using questionmap.clear just in case. I will look into the keyvalue store class, I thought that I could just read map in order with the method used in my example code.

Am I correct in thinking that the code should work though?

Dave

Sent from my Nexus 7
 
Upvote 0

mistermentality

Active Member
Licensed User
Longtime User
I really need some help with this as I found that the routine was not being called, now it is being called and the code shown in my original post simply makes a map with "Item #1", "" and "Item #2","" etcetera then simply appends the read in map data after it.

So my question is why is this code:
B4X:
tempmap = File.ReadMap(File.DirInternal,"questionmap.map")
tempnum = tempmap.Size-1 ' tempnum now holds the size of questionmap, how many items are stored in it

For lp = 0 To tempnum
questionmap.Put("Item #" &lp,"")
Next
questionmap = File.ReadMap2(File.DirInternal,"questionmap.map",questionmap)
'questionmap now contains the contents of saved questionmap in correct order
tempmap.Clear
tempnum = 0

simply appending the data when ReadMap2 is supposed to read the data in order and replace Item#1 with first key, Item#2 with second key, etcetera, isn't it?

When I go to get a key and value from the map, key one is now "Item #1" and not what it was saved as which, as my app uses several maps, is meaning I cannot now make my application work until I can find out why ReadMap2 isn't working yet I followed the example for it.

Dave
 
Upvote 0

mistermentality

Active Member
Licensed User
Longtime User
Can you upload a small program that demonstrates this issue?

I've written this simple app to demonstrate the issue, it just shows two buttons on screen. The first button creates a map and saves it, then shows the contents in a msgbox. Pressing the second button reads in the saved map using ReadMap2 and shows the map as it is when read in using ReadMap2.

I've included the apk, the code for the example app is as follows....

B4X:
#Region  Project Attributes 
   #ApplicationLabel: B4A Example
   #VersionCode: 1
   #VersionName: 
   'SupportedOrientations possible values: unspecified, landscape or portrait.
   #SupportedOrientations: unspecified
   #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes 
   #FullScreen: False
   #IncludeTitle: True
#End Region

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
Dim tempmap, questionmap As Map
Dim tempnum As Int
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.

   Dim readmap_button As Button
   Dim savemap_button 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")

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub readdata
If tempmap.IsInitialized = False Then tempmap.Initialize
If questionmap.IsInitialized = False Then questionmap.Initialize
tempmap.Clear
questionmap.Clear


tempmap = File.ReadMap(File.DirInternal,"questionmap.map")
tempnum = tempmap.Size-1 ' tempnum now holds the size of questionmap, how many items are stored in it
For lp = 0 To tempnum
questionmap.Put("Item #" &lp,"")
Next
questionmap = File.ReadMap2(File.DirInternal,"questionmap.map",questionmap)
'questionmap now contains the contents of saved questionmap in correct order
tempmap.Clear
tempnum = 0
Msgbox(questionmap,"Question map:")
End Sub

Sub savedata
If tempmap.IsInitialized = False Then tempmap.Initialize
If questionmap.IsInitialized = False Then questionmap.Initialize
tempmap.Clear
questionmap.Clear

Dim n As Int
For n = 1 To 20
questionmap.Put("Key word number " & n, "Value number " & n)
Next
File.WriteMap(File.DirInternal,"questionmap.map",questionmap)
Msgbox(questionmap,"Question map:")
End Sub

Sub savemap_button_Click
   savedata
End Sub
Sub readmap_button_Click
   readdata
End Sub

I populate a map (questionmap) with "Item #1" "Item #2" (etcetera) keys and "" values.

That map is later reused to read the saved map data back in using ReadMap2 where I clear the map, create the appropriate number of "Item #" keys and values as per the ReadMap2 example code and then read in the saved map contents.

However what happens is that the saved map data is simply read back in a random order and simply appended to the end of the questionmap after the "Item #1" "Item #2" etc keys and values.

That is what I would expect normally using a put command but as I understand it the command should replace Item #1 with whatever is saved as Key number one.

I hope someone can point out where this is going wrong as if it's simply my mistake I will be a little embarrased but can amend my app accrodingly.

Thanks,

Dave
 

Attachments

  • maptest.apk
    100.4 KB · Views: 167
Upvote 0

mistermentality

Active Member
Licensed User
Longtime User
If it is possible please upload this project (File - Export as zip).

If you mean the small demo app I wrote to show the problem, yes I can. I have attached the zip to this post. It is exactly the same code as the larger app I have the code in, rather than upload the original app code which has lots of additional commands unrelated to the issue I've attached the demo code as it demonstrates the exact problem without the distraction of any additional code.

Thanks

Dave
 

Attachments

  • maptest.zip
    7.2 KB · Views: 182
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The keys must match. In your code you just added another 20 items.

This code will work as expected:
B4X:
Sub readdata
   If tempmap.IsInitialized = False Then tempmap.Initialize
   If questionmap.IsInitialized = False Then questionmap.Initialize
   tempmap.Clear
   questionmap.Clear
   For lp = 1 To File.ReadMap(File.DirInternal,"questionmap.map").Size
      questionmap.Put("Key word number " &lp,"")
   Next
   questionmap = File.ReadMap2(File.DirInternal,"questionmap.map",questionmap)
   tempmap.Clear
   tempnum = 0
   Msgbox(questionmap,"Question map:")
End Sub
 
Upvote 0

mistermentality

Active Member
Licensed User
Longtime User
The keys must match. In your code you just added another 20 items.

This code will work as expected:
B4X:
Sub readdata
   If tempmap.IsInitialized = False Then tempmap.Initialize
   If questionmap.IsInitialized = False Then questionmap.Initialize
   tempmap.Clear
   questionmap.Clear
   For lp = 1 To File.ReadMap(File.DirInternal,"questionmap.map").Size
      questionmap.Put("Key word number " &lp,"")
   Next
   questionmap = File.ReadMap2(File.DirInternal,"questionmap.map",questionmap)
   tempmap.Clear
   tempnum = 0
   Msgbox(questionmap,"Question map:")
End Sub

So readmap2 can only read a saved map in order if you know the original order of the keys?

For example if key ones value was 1 and key two value was 2 then, if in the temporary map used I had put 1 in key 1 and 2 in key 2 it would read keys 1 and 2 in order, so the problem is that I misunderstood the example.

Thanks for helping, as I don't know the maps key order when saved I will look to use a different method. Thank you, you saved me from more hours of puzzlement :)

Dave

Sent from my Nexus 7
 
Upvote 0
Top