Android Question [ SOLVED ] [B4X] Editable B4XTable + Form with time field

jimmyF

Active Member
Licensed User
Longtime User
I have tried just about everything and I can not figure out how to edit the Time value.

No problem setting a default value in btnAdd sub:
B4X:
Sub btnAdd_Click
Dim m As Map = CreateMap()
    m.Initialize
    m.Put("Type","Delivery")
    m.Put("ONum","")
    m.Put("Address","")
    m.Put("Phone","613-")
  
    lperiod.Hours = DateTime.GetHour(DateTime.Now)
    lperiod.Minutes = DateTime.GetMinute(DateTime.Now)
    m.Put("Time",lperiod)
    ShowDialog(Item, RowId)
And it works fine with those defaults set.

But when I try to edit the data, (Called from btnEdit_Click) the programme crashes when it gets to this line in the Sub ShowDialog(Item, RowId):
B4X:
Wait For (PrefDialog.ShowDialog(Item, "OK", "CANCEL")) Complete (Result As Int)

B4X:
Error log:
Error occurred on line: 359 (PreferencesDialog)
java.lang.ClassCastException: java.lang.String cannot be cast to b4a.example.dateutils$_period
    at sef.LPDelivery.preferencesdialog._filldata(preferencesdialog.java:2267)
    at sef.LPDelivery.preferencesdialog$ResumableSub_ShowDialog.resume(preferencesdialog.java:583)
    at sef.LPDelivery.preferencesdialog._showdialog(preferencesdialog.java:405)
    at sef.LPDelivery.editabletable$ResumableSub_ShowDialog.resume(editabletable.java:926)
    at sef.LPDelivery.editabletable._showdialog(editabletable.java:892)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:6302)
    at android.view.View$PerformClick.run(View.java:24908)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6558)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:469)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You cannot add a Period object to B4XTable. You need to convert it to a string and vice versa.

B4X:
'Example: params.AddAll(Array(Item.Get("Type"), Item.Get("Name"), Item.Get("Birth Date"), TimePeriodToString(Item.Get("Time"))))
Sub TimePeriodToString (p As Period) As String
   Return $"$1.0{p.Hours}:$2.0{p.Minutes}"$
End Sub

Sub StringToTimePeriod (s As String) As Period
   Dim m As Matcher = Regex.Matcher("(\d+):(\d+)", s)
   Dim p As Period
   If m.Find Then
       p.Initialize
       p.Hours = m.Group(1)
       p.Minutes = m.Group(2)
   Else
       Log("Invalid time string: " & s)
   End If
   Return p
End Sub

'Call this when you want to get an item from the table.
Sub GetItemFromRow(RowId As Long) As Map
   Dim Item As Map = B4XTable1.GetRow(RowId)
   Item.Put("Time", StringToTimePeriod(Item.Get("Time")))
   Return Item
End Sub
 
Upvote 0
Top