Android Question Multiple CustomViews inside a CustomView all added by code

Gaxapu

Member
Licensed User
Hello all
Just to be sure I´m not doing something wrong or not advisable or merely stupid. This is a skeleton of what I'm doing.
What I want is to create a custom view that goes multiple times inside of another custom view and to generate all this CustomViews by code.
The code seems to work. It is the result of I´mNotGoingToSayHowManyHoursCauseItAshamedMe :rolleyes::) . Trust me, I have searched the forum. All I have found was isolated bits of the answer.
Perhaps I have not typed the right words or have not found the right tutorial :(
Here it goes.

Inside CustomView. Call it cvTiny:
Sub Class_Globals
  Private mEventName      As String 'ignore
  Private mCallBack       As Object 'ignore
  Private mBase           As B4XView 'ignore
  Private xui             As XUI 'ignore

  Private mLinkedData     As classData1
  Private mLabel1         As Label
End Sub

Public Sub Initialize (Callback As Object, EventName As String)
    mEventName = EventName
    mCallBack = Callback

    mLabel1.Initialize("")
End Sub

Public Sub DesignerCreateView (Base As Object, Lbl As Label, Props As Map)
    mBase = Base

    mBase.Tag = Me 'Is this what is called "Code Tag" in some references???
    mBase.AddView(mLabel1, 0, 0, 0, 0)
End Sub

Private Sub Base_Resize (Width As Double, Height As Double)
    mBase.Width     = Width
    mBase.Height    = Height

    mLabel1.SetLayout(5dip, 5dip, mBase.Width - 10dip, mBase.Height - 10dip)
End Sub

Public Sub getLeft As Int
    Return mBase.Left
End Sub

Public Sub setLeft(Value As Int)
    mBase.Left = Value
End Sub

Public Sub getWidth As Int
    Return mBase.Width
End Sub

Public Sub setWidth(Value As Int)
    mBase.Width = Value
    Base_Resize (mBase.Width, mBase.Height)
End Sub

Public Sub getLinkedData As classData
    Return mLinkedData
End Sub

Public Sub setLinkedData(Value As classData1)
    mLinkedData = Value
    mLabel1.Text = mLinkedData.Text
End Sub


Outside CustomView. Call it cvBig:
Sub Class_Globals
    Private mEventName        As String 'ignore
    Private mCallBack        As Object 'ignore
    Private mBase            As B4XView 'ignore
    Private xui                As XUI 'ignore

    Public  mLinkedData        As classData2
    Private mPanels(10)        As Panel
    Private mTinys(10)        As cvTiny
End Sub

Public Sub Initialize (Callback As Object, EventName As String)
    mEventName = EventName
    mCallBack = Callback

    For i=0 To 9
        mPanels(i).Initialize("")
    Next
End Sub

Public Sub DesignerCreateView (Base As Object, Lbl As Label, Props As Map)
    mBase = Base
    mBase.Tag = Me
    CallSubDelayed(Me, "DelayedCreateView") 'Delayed because it needs the view to be completely created before I can use it for the inside views
End Sub

Private Sub DelayedCreateView
    For i=0 To 9
        mBase.AddView(Panels(i), 2dip, 2dip + (i * 50dip), mBase.Width - 4dip, 20dip)
        mPanels(i).LoadLayout("cvTiny")
        mTinys(i) = mPanels(i).GetView(0).Tag
    Next
End Sub

Private Sub Base_Resize (Width As Double, Height As Double)
    mBase.Width        = Width
    mBase.Height    = Height
End Sub

Public Sub getLinked As classData2
    Return mLinkedData
End Sub

Public Sub setLinkedData(Value As classData2)
    mLinkedData = Value
    For i=0 To 9
        mTinys(i).LinkedData = mLinkedData.mData(i)
    Next
End Sub

And now the questions:
Is this the right way of doing?
Is the line highlighted en cvTiny what I he found to be called "Code Tag" and is that correctly used?
Why is it not recommended to use a function inside the CustomView to add the view to a parent as I have seen by LucaMs and others?
And the big one (although I have already seen the Erel´s answer in another thread): Why not include an AddView method to CustomViews? More a wish than a question I suppose.

Thank you in advance.
 

DonManfred

Expert
Licensed User
xclv should be added with a Layout.

Create a Layout just with the xCLV and load this layout to a panel whenever you need one.
 
Upvote 0

Gaxapu

Member
Licensed User
xclv should be added with a Layout.

Create a Layout just with the xCLV and load this layout to a panel whenever you need one.
¿XCLV? I am not using XCLV.
If you look at it, using a panel and loading the customview IS what I am doing. Is the case with the cvTiny inside the cvBig, and how I load cvBig if it is not added in the visual designer.

Thanks for your answer.
 
Upvote 0

Gaxapu

Member
Licensed User
Any warm soul willing to help?

I'm having troubles with the delayed sub of create view. Sometimes it is not called before I try to assign properties to the new object and the App crashes because of that.

Any help will be appreciated.
 
Upvote 0

Gaxapu

Member
Licensed User
You can remove the DelayedCreateView sub, put Sleep(0) and then continue with this code.

You haven't posted the error message and you haven't posted the code that fails so it is difficult to help you.

Add Sleep(0) after you load the custom view and before you try to access any of its inner views.

You are right. I have not given enough info. Sorry for that.

I have modified cvBig's code as this:
Modified cvBig:
Public Sub DesignerCreateView (Base As Object, Lbl As Label, Props As Map)
    mBase = Base
    mBase.Tag = Me
    'CallSubDelayed(Me, "DelayedCreateView") 'Delayed because it needs the view to be completely created before I can use it for the inside views
    Sleep(0)
    DelayedCreateView
End Sub
I suppose it doesn't matter to call a sub instead of include the code.
When the App crash, the sub "DelayedCreateView" was never called.

Error message: java.lang.RuntimeException: Class instance was not initialized (cvBig)

If I understand correctly, I should put two Sleeps. One on the CreateView sub, another one after each load of the customview. Is that so?
After putting the two sleeps the App seems not to crash. Problem is I don´t know why.

Note: I have not been able to reproduce the error with just the simple skeleton provided in the example. Hope you excuse me to put all my code.

How about the questions in the first post?

Many thanks for the help.
 
Upvote 0
Top