Android Tutorial Save all Edittexts & corresponding labels on a view to a file (JSON) and reload them

KMatle

Expert
Licensed User
If you build an app which uses a larger form you might have realized this by pairs of labels and edittexts. The label ist used for the description (f.e. "Name") and the edittext will contain the correspondig data ("Smith").

Having 10 or more pairs is sometimes annoying to handle (or you are lazy like me). So here I will show how to do this automatic.

This may be done in many ways but here you just call the sub's. Everything else is done automatically.

Save:

- a small loop gets all views of a scrollviews' panel (could be anything else containing labels and edittexts)
- assuming you always have a matching pair of label & edittext, all the pairs are stored in a String
- this string will be written to a file (here I use randomaccess including a password)

B4X:
Sub save_filled
         Dim et As EditText, lbl As Label
          Dim JSONMap As Map  
        JSONMap.Initialize
      
        For i=0 To sv.Panel.NumberOfViews-1 Step 2
            If sv.Panel.GetView(i) Is Label Then
              lbl=sv.Panel.GetView(i)
              Log(lbl.Text)
            End If
            If sv.Panel.GetView(i+1) Is EditText Then
              et=sv.Panel.GetView(i+1)
              Log("ET:"&et.Text)
            End If
          
            JSONMap.Put(lbl.Text,et.Text)
        Next
      
        Dim JSONGenerator As JSONGenerator
        JSONGenerator.Initialize(JSONMap)
        Dim jsonstring As String
        jsonstring=JSONGenerator.ToString
      
        Dim raf As RandomAccessFile
        raf.Initialize(MyPath, MyFileName&".fil", False)
        raf.WriteEncryptedObject(jsonstring, "Password", raf.CurrentPosition)
        raf.Close
        End Sub

Load:

- the JSON string is read from the file
- a loop gets all the views
- if it finds a label, the text will be used to get the matching pair in the JSON string
- example: If label.text is "Name" then "Name" will be searched in the JSON structure and it returns the value behind it
- the value will be put back again to the edittext
- cool: the textsizes are set to the optimal value

B4X:
Sub load_filled
  
    Dim raf As RandomAccessFile
    Dim Jsonstring As String
    raf.Initialize(MyPath, MyFileName&".fil", False)
  
    Jsonstring = raf.ReadEncryptedObject("Password", raf.CurrentPosition)
    raf.Close
      
    Dim parser As JSONParser
    parser.Initialize(Jsonstring)
    Dim fmap As Map
    fmap = parser.NextObject
  
    Dim lbl As Label, et As EditText
    For i=0 To sv.Panel.NumberOfViews-1 Step 2
        If sv.Panel.GetView(i) Is Label Then
          lbl=sv.Panel.GetView(i)
          Log(fmap.Get(lbl.Text))
          If sv.Panel.GetView(i+1) Is EditText Then
              et=sv.Panel.GetView(i+1)
              et.Text=fmap.Get(lbl.Text)
              SetEditTextSize(et, et.Text)
          End If
        End If
      
    Next
End Sub
B4X:
Sub SetEditTextSize(ex As EditText, txt As String)
   
    Dim dt As Float
    Dim limit = 0.5 As Float
    Dim h As Int
    Dim stu As StringUtils
   
    ex.Text = txt
    ex.TextSize = 72
    dt = ex.TextSize
    h = stu.MeasureMultilineTextHeight(ex, txt)   
    Do While dt > limit OR h > ex.Height
        dt = dt / 2
        h = stu.MeasureMultilineTextHeight(ex, txt)   
        If h > ex.Height Then
            ex.TextSize = ex.TextSize - dt
        Else
            ex.TextSize = ex.TextSize + dt
        End If
    Loop
    ex.TextSize=ex.textsize *0.8

End Sub
 
Top