Android Question Panel.AddView(<customview>,...) error


Licensed User
Longtime User
Hi all. I searched a lot for this issue I'm having right now but, a part from the fact it's not easy to "clarify" it in a Google textbox, I found nothing.

I'm using a CustomListView that's a CustomView with designer support. The fact is I have to add a new CustomListView to a panel at run time.

I thought it wasn't a problem, but I'm getting a compile error when trying:

Panel.AddView(cListView, 0, 0, Panel.Width, Panel.Height)

and the error is:
error: incompatible types: customlistview cannot be converted to View

So, a part from the fact I was thinking that customviews were in fact views ... there's a solution?

As usual, thank you so much.


Licensed User
Longtime User
Just to give an idea.

An old version of CustomListView contains:
Public Sub Initialize (vCallback As Object, vEventName As String)
    sv.Initialize2(0, "sv")
    dividerHeight = 2dip
    EventName = vEventName
    CallBack = vCallback
    sv.Color = 0xFFD9D7DE 'this sets the dividers color
    Dim r As Reflector
    Dim idPressed As Int
   idPressed = r.GetStaticField("android.R$drawable", "list_selector_background")
   r.Target = r.GetContext
   r.Target = r.RunMethod("getResources")
    pressedDrawable = r.RunMethod2("getDrawable", idPressed, "")
    DefaultTextColor = Colors.White
    DefaultTextSize = 16
    DefaultTextBackgroundColor = Colors.Black
    DefaultTextBackground = Null
End Sub

With that code you could add the CustomListView to a Panel or to an Activity (vCallBack).

I don't know if adding this routine to the CustomListview you may have problems... try.
Upvote 0


Licensed User
Longtime User
You need to add a routine in the class to add the CustomListView to a parent like:
Public Sub AddToParent(Parent As Panel, Left As Int, Top As Int, Width As Int, Height As Int)
    Parent.AddView(sv, Left, Top, Width, Height)
End Sub
And in the calling activity something like:
clv2.Initialize(Me, "clv2")
clv2.AddToParent(Activity, 10dip, 50%y, 100%x - 20dip, 50%y - 10dip)
You may have a look at chapter 12.4 Custom views in the B4A User's Gude.
And especially chapter 12.4.2 Adding a custom view by code.
There you find an example of a CustomView which can also be added in the code.
Upvote 0