Not understanding maps properly

Smee

Well-Known Member
Licensed User
Longtime User
I have read and read and written until i am confused

All i am trying to do is write a couple of values and then re-read them

The code is as follows

B4X:
Sub Activity_Resume
Dim m As Map
m.Initialize
m.Put("PageNumber","")
m.Put("CatNum","")
If File.Exists(File.DirInternal, "Settings.txt") Then
   m = File.ReadMap2(File.DirInternal, "Settings.txt", m)
End If

Log(PageNumber)
Log(CatNum)

end sub

Sub Activity_Pause (UserClosed As Boolean)
Dim m As Map
m.Initialize
m.Put("PageNumber", PageNumber)
m.Put("CatNum", PageNumber)
File.WriteMap(File.DirInternal, "Settings.txt",m)

End Sub

So where am i going wrong? I have tried many variations of assigning values etc. But i just am not getting it

Thanks all
 

klaus

Expert
Licensed User
Longtime User
You don't say what your problem is.

But try this code:
B4X:
Sub Activity_Resume
  Dim m As Map
  m.Initialize
  If File.Exists(File.DirInternal, "Settings.txt") Then
    m = File.ReadMap(File.DirInternal, "Settings.txt")
    [COLOR=red]PageNumber = m.Get("PageNumber")[/COLOR]
[COLOR=red]  CatNum = m.Get("CatNum")[/COLOR]
  End If
 
  Log(PageNumber)
  Log(CatNum)
End sub
 
Sub Activity_Pause (UserClosed As Boolean)
  Dim m As Map
  m.Initialize
  m.Put("PageNumber", PageNumber)
  m.Put("CatNum", [COLOR=red]CatNum[/COLOR])
  File.WriteMap(File.DirInternal, "Settings.txt",m)
End Sub
You set the same value to the two different entries.
You must set the map entries to the variables with m.Get.

You could also save the two values in a text file and read them back instead of using the maps.

Best regards.
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
Thanks for the reply

You have not assigned these after you come back from resume.
you should do PageNumber = m.Get("PageNumber")

I actually had tried that variation earlier and i got the following error

java.lang.NumberFormatException:

Joe
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
You don't say what your problem is.

You could also save the two values in a text file and read them back instead of using the maps.

Best regards.

Yes that is true but i want to learn how to use the map syntax correctly

The problem was that I was unable to save values when moving between activities. The values were being wiped out even thought they were transfered to the main module. So i wAs looking for alternate solutions.

Thanks for the help Klaus, The changed code now works:icon_clap:
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
The problem was that I was unable to save values when moving between activities. The values were being wiped out even thought they were transfered to the main module. So i wAs looking for alternate solutions.
If you declare the two variables in Sub Process_Globals their content will be maintained when you swap activities.
In that case, you need to save the values in a file only if you need the values of the last run.

Best regards.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…