This may seem like a simple question and should have a simple answer, but I'm at a loss for finding a simple solution. I hope I am missing something obvious.
Scenario:
I have a form with a couple dozen views on it of different types. They could be the TextField, TextArea, ComboBox, DatePicker, CheckBox, Button, B4XViews etc..
I want to get their value and store the view's name and value in a map. Traversing the views on the form to get all the views is easy enough. My problem is the views can have a different property to access the view's value. I was hoping I could use something like:
private locVal as String = B4XView1.Text
but not all B4XViews will use the Text property. The DatePicker view will use DatePicker1.DateTicks to return the value. If I try and reference DatePicker1.Text it throws an exception.
Solution?
The only solution I can come up with is to create a sub that checks for each type of possible views and retrieve the view's value by explicitly assigning the aObject to that view type so I can reference its proper value property. I would have subs that would identify if the view has a text property like "ViewHasTextProperty" and another sub to return that property "GetViewText". And I would have to do this for Integer values, DateTick values, etc..
This can get quite involved. Is there an easier way to retrieve the value from any view that is passed as an argument to a sub?
It should be cross platform compliant (B4J,B4A,B4i).
TIA
Scenario:
I have a form with a couple dozen views on it of different types. They could be the TextField, TextArea, ComboBox, DatePicker, CheckBox, Button, B4XViews etc..
I want to get their value and store the view's name and value in a map. Traversing the views on the form to get all the views is easy enough. My problem is the views can have a different property to access the view's value. I was hoping I could use something like:
private locVal as String = B4XView1.Text
but not all B4XViews will use the Text property. The DatePicker view will use DatePicker1.DateTicks to return the value. If I try and reference DatePicker1.Text it throws an exception.
Solution?
The only solution I can come up with is to create a sub that checks for each type of possible views and retrieve the view's value by explicitly assigning the aObject to that view type so I can reference its proper value property. I would have subs that would identify if the view has a text property like "ViewHasTextProperty" and another sub to return that property "GetViewText". And I would have to do this for Integer values, DateTick values, etc..
This can get quite involved. Is there an easier way to retrieve the value from any view that is passed as an argument to a sub?
It should be cross platform compliant (B4J,B4A,B4i).
TIA
B4X:
'------------- ViewText -----------
Sub ViewHasTextProperty(aObject As Object) As Boolean
Private Result As Boolean=False
' Private locB4XView As B4XView
If aObject <> Null Then
If aObject Is B4XView Then
#IF B4J
Result = aObject Is TextField Or aObject Is Button Or aObject Is TextArea Or aObject Is Label Or aObject Is CheckBox Or aObject Is RadioButton Or aObject Is ToggleButton
#END IF
#IF B4A
Result = aObject Is EditText or aObject is Button Or aObject Is Label Or aObject Is CheckBox Or aObject Is RadioButton Or aObject Is ToggleButton
#END IF
#IF B4i
Result = aObject Is TextField or aObject is Button Or aObject Is Label Or aObject Is TextView
#End If
End If
End If
Return Result
End Sub
Sub GetViewText(aObject As Object) As String
Private locType As String = GetType(aObject)
Log(locType)
If aObject <> Null Then
If aObject Is B4XView Then
Private locB4XView As B4XView = aObject
If ViewHasTextProperty(locB4XView) Then
Private locStr As String = locB4XView.Text
Log("B4XView.Text = " & locStr)
Return locStr
Else
...