Android Question How to convert generic View object to a CustomView?

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
I've created a custom view in a library called "MyCustomView". I've added the library to a new project and then added the customview to a layout file.

I set the tag property in the designer to "myView" on the custom view. This layout file containing the CustomView from the library is loaded into various panels on a scroll view.

At a future point, I need to iterate though the views on the panel looking for the custom view and then access custom properties on the custom view:
B4X:
    Dim poLabel As Label
    Dim poImv As ImageView
    Dim poMyCV As MyCustomView

    For Each poView As View In oPan.GetAllViewsRecursive
        Dim psTag As String = poView.Tag
        Select Case psTag
            Case "myLabel"
                poLabel = poView
            Case "myImage"
                poImv = poView
            Case "myView"
                poMyCV = poView
        End Select
    Next
   
    poLabel.Text = "New label text"
    poMyCv.MyProperty = "New custom value"

This works fine for the regular views, however I get an error compiling when I try to assign the CustomView:
B4X:
poMyCV = poView
javac 1.8.0_101
src\phx\project\main.java:800: error: incompatible types: View cannot be converted to mycustomview
_pomycv = (phx.mycustomview.mycustomview)(_poview.getObject());

How can I coerce the "View" object type into my customview type?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
poView will never be the custom view class. It will always be the view that you added in your class.

You should set a reference to the class instance:
B4X:
Public Sub DesignerCreateView(base As Panel, lbl As Label, props As Map)
'...
 View.Tag = Me
End Sub

Later you can find it with:
B4X:
If poView.Tag Is MyCustomView Then
 Dim cv As MyCustomView = poView.Tag
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
So, if "mBase" in the customview is a panel then the object type of the customview will forever be a panel?

I'm currently using the view's Tag property (all views on the layout, actually) to store an identifier that I can use to distinguish between them. And what if I have multiple copies of the customview on the layout; this solution would not allow me to differentiate between them.

I suppose I can add a custom "Name" property to the view and check that instead of the Tag property if the Type of the object's Tag matches, and then check the rest of the items' Tag properties if not.

Is there a way to prevent the "Tag" property from showing up in the Designer on a customview? If I'm going to be relying on it I don't want to be able to override it.

This seems unnecessarily complex. Is there no java equivalent to VB.Net's DirectCast and/or TryCast?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
So, if "mBase" in the customview is a panel then the object type of the customview will forever be a panel?
Yes. This is the view that is added to the layout.

Is there no java equivalent to VB.Net's DirectCast and/or TryCast?
It is not related to casting as you cannot cast a Panel (or any other view) to your class.

You can use a custom type for Tag property value and store whichever information you need. You can also include a field with the designer tag property. It is not possible to hide.
 
Upvote 0
Top