Android Question RandomAccessFile Boolean

Discussion in 'Android Questions' started by jaraiza, Feb 24, 2015.

  1. jaraiza

    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
     
  2. sorex

    sorex Expert Licensed User

    you can always cast it to another type

    Code:
    Dim b As Boolean
    Dim t As String
    t=b
    Log(t)
    why not use StateManager to store form and other settings?
     
    KZero likes this.
  3. KZero

    KZero Active Member Licensed User

    to save
    Code:
    Dim sVal As String
    If Radiobutton1.Checked=True Then sVal ="1" Else sVal="0"
    File.WriteString(File.DirInternal,"filename.txt",sVal)
    to load
    Code:
    Dim sval As String
    sval = 
    File.ReadString(File.DirInternal,"filename.txt")
    If sval="1" Then Radiobutton1.Checked=True Else Radiobutton1.Checked=False
     
  4. jaraiza

    jaraiza Active Member Licensed User

    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: Feb 25, 2015
  5. jaraiza

    jaraiza Active Member Licensed User

    Because I don't feel comfortable using space I won't need, I just need to write 1 or 2 variables.
     
  6. sorex

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

    Erel Administrator Staff Member Licensed User

    Post your code. RandomAccessFile supports boolean (as well as strings and almost all other non-UI types).
     
  8. jaraiza

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

    Code:
    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 :)
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice