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.
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.
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: