Android Question RandomAccessFile Boolean

jaraiza

Active Member
Licensed User
Hi,

I need to store the last Checked status from a radioButton. The easiest way to do it was to save a Boolean variable but it seems it's not supported by RandomAccessFile lib.

I tried to use WriteObject to save the boolean value, but after a successfully save when I use ReadObject I get this error: java.lang.NegativeArraySizeException: -1

Do I need to use a string equivalent or something? Like saving "true" or "false" ?

Thanks
 

sorex

Expert
Licensed User
you can always cast it to another type

B4X:
Dim b As Boolean
Dim t As String
t=b
Log(t)
why not use StateManager to store form and other settings?
 

KZero

Active Member
Licensed User
to save
B4X:
  Dim sVal As String
If Radiobutton1.Checked=True Then sVal ="1" Else sVal="0"
File.WriteString(File.DirInternal,"filename.txt",sVal)
to load
B4X:
Dim sval As String
sval = File.ReadString(File.DirInternal,"filename.txt")
If sval="1" Then Radiobutton1.Checked=True Else Radiobutton1.Checked=False
 

jaraiza

Active Member
Licensed User
to save
B4X:
  Dim sVal As String
If Radiobutton1.Checked=True Then sVal ="1" Else sVal="0"
File.WriteString(File.DirInternal,"filename.txt",sVal)
to load
B4X:
Dim sval As String
sval = File.ReadString(File.DirInternal,"filename.txt")
If sval="1" Then Radiobutton1.Checked=True Else Radiobutton1.Checked=False
Yeah, that was exactly what I meant with using the string equivalent "true" instead of boolean true.

The problem is RandomAccessFile doesn't support strings. Well, I'll cast to another type.

Thanks!
 
Last edited:

jaraiza

Active Member
Licensed User
you can always cast it to another type

B4X:
Dim b As Boolean
Dim t As String
t=b
Log(t)
why not use StateManager to store form and other settings?
Because I don't feel comfortable using space I won't need, I just need to write 1 or 2 variables.
 

sorex

Expert
Licensed User
I think it wouldn't hurt to check out StateManager as it's very easy to use (just putsetting(settingname,value) and getsetting(settingname)) and you'll need it soon anyway in another project.
 

jaraiza

Active Member
Licensed User
Well, something funny has just happened :)

As I've already changed my code to use "byte" type (and it was already working), I rewrote the code to use objects again to check the error so I can post it.

And... Now it works. I suppose I did something wrong before :oops:

Just to leave the working code here:

----------------
I've 2 radiobuttons in the designer, "rdTOK_SI" and "rdTOK_NO" (inside a panel named "pnl_rdTOK"), mutually exclusive. Also there is a button named "btnOK".

B4X:
Sub Globals
    Dim rConfig As RandomAccessFile
    Dim rdTOK_Val As Boolean
    Dim readVar As Boolean = True
    Dim DummyStatus As String

    Private pnl_rdTOK As Panel
    Private rdTOK_SI As RadioButton
    Private rdTOK_NO As RadioButton
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Opts")
   
    rConfig.Initialize(File.DirInternal,"MyCFG.dat",True)
    Try
        rdTOK_Val = rConfig.ReadObject(rConfig.CurrentPosition)
    Catch
        readVar = False
    End Try

    If readVar Then
        rdTOK_SI.Checked = rdTOK_Val
        rdTOK_NO.Checked = Not(rdTOK_Val)
    End If
    rConfig.Close
End Sub

Sub btnOK_Click
    rConfig.Initialize(File.DirInternal,"MyCFG.dat",False)
    rConfig.WriteObject(rdTOK_SI.Checked,True,rConfig.CurrentPosition)
    rConfig.Close
    Activity.Finish
End Sub
Thanks anyway :)
 
Top