Android Question Initialize XCustomListView

AlpVir

Well-Known Member
Licensed User
I would like to use XCustomListView without (I stress without) the Designer.
I am able to add panels without using the Designer.
B4X:
Dim p As B4XView = xui.CreatePanel ("")
....
But I can't understand the statment
clv2.initialize (VCallBack as object, vEventName as string) as string
In other words, I would NOT like to follow Erel's directions that involve using the Designer. I would like to do everything with code.
Customviews should be loaded from a layout.
You can create a layout with just the xclv and use this layout whenever you need a clv.
It's possible ?
Thanks in advance
 

DonManfred

Expert
Licensed User
 

AlpVir

Well-Known Member
Licensed User
Thanks for the tip but that's not what I was asking.
I am already able to add B4XView type panels.
But I don't know how to add, with code, the Custum View "clv2".
In other words by deleting the line
B4X:
Activity.LoadLayout ("1")
the following example no longer works

B4X:
Sub Globals
    Private clv2 As CustomListView
    Private xui As XUI
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
      Dim NomeAttivita(5) As String
    Dim Pre(5)            As String
    Dim Celeste            As Long
    Dim Sfondo             As Long
    NomeAttivita(0)="SCIALPINISMO":NomeAttivita(1)="MOUNTAIN BIKE":NomeAttivita(2)="ESCURSIONISMO":NomeAttivita(3)="BICI DA STRADA"
    Pre(0)="uno" & CRLF & "a capo":Pre(1)="alfa":Pre(2)="beta":Pre(3)="ultimo" & CRLF & "a capo"
    Celeste=Colors.Blue
    For i = 0 To 3
        'clv2.Add(CreateListItem($"Item #${i}"$, clv2.AsView.Width, 60dip,1234), $"Item #${i}"$)
        clv2.Add(AggiungiAttivitaXUI (i,NomeAttivita(i),Pre(i), Sfondo) , i)
    Next
End Sub

Sub AggiungiAttivitaXUI(N As Int, Nome As String, Preferenze As String, Sfondo As Long) As Panel
    Dim p As B4XView = xui.CreatePanel("")
    P.Color=Sfondo
    p.Height=55dip
    '--- icona
    Dim IV As ImageView
    IV.Initialize ("")
    Select Case N
        Case 0:    IV.Bitmap=LoadBitmap(File.DirAssets,"skialp.png")
        Case 1:    IV.Bitmap=LoadBitmap(File.DirAssets,"trekking.png")
        Case 2:    IV.Bitmap=LoadBitmap(File.DirAssets,"mtb.png")
        Case 3:    IV.Bitmap=LoadBitmap(File.DirAssets,"bike.png")
    End Select        
    IV.Gravity=Gravity.fill
    P.AddView (IV,5dip,5dip,45dip,45dip)
    '--- Nome
    Dim Lb As Label
    Lb.Initialize("")
    If Activity.Height>800 Then
        Lb.Gravity = Bit.Or(Gravity.Left, Gravity.CENTER_VERTICAL)
        Lb.TextSize = 16
    Else
        Lb.Gravity = Bit.Or(Gravity.Left, Gravity.top)
        Lb.TextSize = 14
    End If        
    Lb.Text = Nome
    Lb.Color = Sfondo :    Lb.TextColor = Colors.Black :    Lb.TextSize = 16 :    Lb.Typeface = Typeface.DEFAULT_BOLD
    P.AddView(Lb, 63dip,  0dip,  100%x-8dip , 53dip)
    '--- tempi
    Dim Lb As Label
    Lb.Initialize("")
    Lb.Text = Preferenze :    Lb.Color = Sfondo :    Lb.TextColor = Colors.Black : Lb.Typeface = Typeface.DEFAULT
    If Activity.Height>800 Then
        Lb.Gravity = Bit.Or(Gravity.Left, Gravity.CENTER_VERTICAL)
        P.AddView(Lb, 205dip,0dip, 100%x, 53dip)
        Lb.TextSize = 11
    Else
        P.AddView(Lb, 63dip,20dip, 100%x, 53dip)
        Lb.Gravity = Bit.Or(Gravity.Left, Gravity.top)
        Lb.TextSize = 11
    End If    
    Return P
End Sub
 

AlpVir

Well-Known Member
Licensed User
In a nutshell: can XUI be used without the Designer? If so, how?
In the example I reported, if I delete that instruction, nothing works anymore. It may be a mistake not to use the Designer, but it is the way I work; I prefer it like this, I'm used to this way. Using the Designer would be a difficult addition for me.
Thanks for your patience
 

Erel

Administrator
Staff member
Licensed User
can XUI be used without the Designer?
XUI has nothing to do with this thread. We are talking about custom views.

Using the Designer would be a difficult addition for me.
Ok. I will create a layout for you that you can load whenever you want to add a CustomListView.

Whenever you need a different custom view, then ask me and I will prepare such layout file. It takes exactly 7 seconds to do.

B4X:
Sub CreateCLVProgrammaticallyWithoutUsingComplicatedDesigner As CustomListView
Dim p As B4XView = xui.CreatePanel("")
p.SetLayoutAnimated(0, 0, 0, 200dip, 200dip)
p.LoadLayout("CLV")
CustomListView1.AsView.RemoveViewFromParent 'declare it as a global variable
Return CustomListView1
End Sub
 

Attachments

Last edited:

Alexander Stolte

Expert
Licensed User
Ok. I will create a layout for you that you can load whenever you want to add a CustomListView.
Nice move ;)

But if you really want to add a xCustomListView without a layout file, then you need this code:
B4X:
Dim tmplbl As Label
tmplbl.Initialize("")
 
Dim tmpmap As Map
tmpmap.Initialize
tmpmap.Put("DividerColor",xui.Color_White)
tmpmap.Put("DividerHeight",5dip)
tmpmap.Put("PressedColor",0x00000000)
tmpmap.Put("InsertAnimationDuration",0)
tmpmap.Put("ListOrientation","Vertical")
tmpmap.Put("ShowScrollBar",True)
    
xclv_main.Initialize(Me,"xclv_main")
xclv_main.DesignerCreateView(mBase,tmplbl,tmpmap)
 

Mahares

Well Known Member
Licensed User
I will create a layout for you that you can load whenever you want to add a CustomListView.
I tried Erel's method for creating xClv programmatically with this code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    CreateCLVProgrammaticallyWithoutUsingComplicatedDesigner
    Activity.LoadLayout("CLV")   'I had to add this line for it to work
    Dim MyList As List
    MyList.Initialize
    MyList = Array ("Apple", "Banana", "Cherry", "Coconut", "Grapes", "Lemon", "Mango", "Melon", _
    "Orange", "Pear", "Pineapple", "Strawberry", "Litchi", "Watermelon", "Tomatoes", "Cherries", "Grapefruit" , _
    "Dates", "Lemon", "Berries", "Raisins", "Pomegranate" )
    MyList.SortCaseInsensitive(True)
    For Each s As String In MyList
        CustomListView1.AddTextItem(s, s)
    Next
End Sub
If I comment this line: Activity.LoadLayout("CLV") I get a blank screen.
If I comment the sub: CreateCLVProgrammaticallyWithoutUsingComplicatedDesigner, and uncomment : Activity.LoadLayout("CLV"), it works as intended, but it is no longer considered created programmatically.
I have to be missing some basic logic.
By the way Erel's sub line p.SetLayoutAnimated(0, 0, 200dip, 200dip) should be:
B4X:
p.SetLayoutAnimated(0, 0, 0, 200dip, 200dip)
. Once in a while we have to expose his human side.
 

Erel

Administrator
Staff member
Licensed User
By the way Erel's sub line p.SetLayoutAnimated(0, 0, 200dip, 200dip) should be:
That's true and fix. It is just a small example that the developer needs to take and change as needed (different size for example).

If I comment this line: Activity.LoadLayout("CLV") I get a blank screen.
Of course. The purpose is to programmatically create a CLV. It is up to the developer to do whatever they like with the new CLV.
 

Mahares

Well Known Member
Licensed User
The purpose is to programmatically create a CLV. It is up to the developer to do whatever they like with the new CLV.
Once created, I like to populate it with the list I have in post 9, but it does not populate the list, so we are back to square one which is the designer way, if I want it to work.
 

Mahares

Well Known Member
Licensed User
ActivityOrRoot.AddView(clv.AsView, 0, 0, 200dip, 200dip)
Now, you are cooking. This works:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    CreateCLVProgrammaticallyWithoutUsingComplicatedDesigner
    Activity.AddView(CustomListView1.AsView, 0, 0, 200dip, 200dip)
    Dim MyList As List
    MyList.Initialize
    MyList = Array ("Apple", "Banana", "Cherry", "Coconut", "Grapes", "Lemon", "Mango", "Melon", _
    "Orange", "Pear", "Pineapple", "Strawberry", "Litchi", "Watermelon", "Tomatoes", "Cherries", "Grapefruit" , _
    "Dates", "Lemon", "Berries", "Raisins", "Pomegranate" )
    MyList.SortCaseInsensitive(True)
    For Each s As String In MyList
        CustomListView1.AddTextItem(s, s)
    Next
End Sub
I can say now: You definitely can create a viable xClv programmatically. @AlpVir should be now happy. Maybe we should call it pseudo-programmatically because you still have to create a layout for xClv in the designer
 
Last edited:
Top