B4J Question Is there an easy way to get a view's value?

Diceman

Active Member
Licensed User
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



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

Cableguy

Expert
Licensed User
Longtime User
you could create a map, with the views name as key and a custom type like (view type, return type) as value, oe simply the use return type, and just check the tag value of the view, wihch you should have entered equal to the views name.
then its a simple mater of lookup the tagvalue in the maps key to know what type of value to expect
 
Upvote 0

Diceman

Active Member
Licensed User
I was hoping to write a class that would do all that for me. I'd pass the Pane or Panel or Activity to a sub and it would go through all of the views and add them to a map, extract their value and data type. I don't see any easy way to extract the view value without hardcoding every possible view class in an "if then else" structure. With Delphi's class inheritance I could reference the view's base component and get its value which is the same property for all of the components built from it. I was hoping there was a way to achieve that with B4X. But I guess not.
 
Upvote 0
Top