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

StefanoAccorsi

Member
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:

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

and the error is:
B4X:
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.
 

LucaMs

Expert
Licensed User
Longtime User
Just to give an idea.

An old version of CustomListView contains:
B4X:
Public Sub Initialize (vCallback As Object, vEventName As String)
    sv.Initialize2(0, "sv")
    items.Initialize
    panels.Initialize
    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, "java.lang.int")
    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

klaus

Expert
Licensed User
Longtime User
You need to add a routine in the class to add the CustomListView to a parent like:
B4X:
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:
B4X:
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
Top