Android Question CustomListView example

zetadan

Member
Licensed User
Longtime User
I am trying to implement the code in Erel's CustomListView tutorial but I cannot understand something.
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    clv1.AddTextItem("Aaaa", "a")
    clv1.AddTextItem("Aaaa" & CRLF & "Bbbb", "b")
    clv1.AddTextItem("Aaaa" & CRLF & "Bbbb" & CRLF & "Cccc", "c")
    clv1.AddTextItem("Aaaa" & CRLF & "Bbbb" & CRLF & "Cccc" & CRLF & "Dddd" , "d")
    clv1.AddTextItem("Aaaa" & CRLF & "Bbbb" & CRLF & "Cccc" & CRLF & "Dddd" & CRLF & "Eeee", "e")
   
    'Second list is created programmatically.
    'Create 20 items made of a label, button and checkbox.
    clv3.Initialize(Me, "clv3")
    Activity.AddView(clv3.AsView, 0, 50%y, 100%x, 50%y)
    For i = 1 To 10
        clv3.Add(CreateListItem("Item #" & i, clv3.AsView.Width, 50dip), 50dip, "Item #" & i)
    Next
End Sub

Sub clv1_ItemClick (Index As Int, Value As Object)
    Activity.Title = Value
End Sub

Sub clv3_ItemClick(Index As Int, Value As Object)
    Log(Index & " = " & Value)
End Sub

Sub CreateListItem(Text As String, Width As Int, Height As Int) As Panel
    Dim p As Panel
    p.Initialize("")
    p.Color = Colors.Black
    Dim b As Button
    b.Initialize("button") 'all buttons click events will be handled with Sub Button_Click
    Dim chk As CheckBox
    chk.Initialize("chk")
    Dim lbl As Label
    lbl.Initialize("")
    lbl.Gravity = Bit.OR(Gravity.CENTER_VERTICAL, Gravity.LEFT)
    lbl.Text = Text
    lbl.TextSize = 16
    lbl.TextColor = Colors.White
    b.Text = "Click"
    p.AddView(lbl, 5dip, 2dip, 150dip, Height - 4dip) 'view #0
    p.AddView(b, 155dip, 2dip, 110dip, Height - 4dip) 'view #1
    p.AddView(chk, 280dip, 2dip, 50dip, Height - 4dip) 'view #2
    Return p
End Sub
I don't fully understand the customlistview add line. After the sub Createlistitem has created the panel there is the item height and then
B4X:
"Item #" & i
Isn't this already in the panel? What is the purpose of this field?
 

mangojack

Expert
Licensed User
Longtime User
If you look closely, the line calling Sub ' clv3.Add ' passes 3 parameters / arguments ..
The first is the Panel (and child views) that is created from the call to sub ' CreateListItem ' (whose arguments are contained within the brackets.. the first being 'Item # & i , which becomes the Label.Text content)
Second is the the Item Height .. (50dip)
And finally the RowID ... "Item # & i" which will be referenced by runtime row click events.

B4X:
Public Sub Add(Pnl As Panel,ItemHeight As Int,RowID As Int )

   InsertAt (items.Size,Pnl,ItemHeight,RowID)

End Sub

this can all be modified to suit.
for instance , I populate the RowID parameter with the Database ID so on user RowClick I can easily Query more data ... similar to the ListView AddTwoLines2 method

just to add .. the first "Item # & i" you see is passed to the 'CreateListItem' Sub as the 'Text' argument.. here you could pass an String or even Object array to supply data to numerous views that you could load into the panel from that sub ..

alter the text of one "Items" to see the result ...
hope this explanation clears the fog ..
 
Last edited:
Upvote 0

zetadan

Member
Licensed User
Longtime User
Mangojack,

Thanks so much. I thought it was something like that but wasn't sure. Your explanation was great.

Dan
 
Upvote 0
Top