Android Question Custom List - Update label text for a specific item in the list.


Active Member
Licensed User

I am using a custom list view that presents labels and push buttons. The labels are filled from a database.

I wish to change the data for a specific item in the list and have it updated.
List Builder:
Sub CreateListItem(Width As Int, Height As Int, row() As Object) As Panel
    Dim p As Panel
    p.SetLayout(0, 0, Width, Height) 'set the panel size before you load the layout.
    'The above line will fail on B4A < v7.30. You should instead add the panel to the Activity with Activity.AddView and remove
    'it after calling LoadLayout with p.RemoveView.
    btnStart.Tag = 0

    Dim titleStr As String = row(0) & " - " & row(2)
    titleStr         = titleStr.Trim
    lblExDesc.Text     = titleStr
    lblStartTime.Text = " - "
    lblStartTime.TextColor = 0xFF000000
    lblEndTime.Text = " - "
    lblEndTime.TextColor = 0xFF000000

    lblTargetWeight.Text = row(3) & " kg"
    lblTargetReps.Text      = "Rps " & row(4)
    lblTargetLifts.Text  = "Sets " & row(5)
    lblNewWeight.Text     = lblTargetWeight.Text
    lblNewLifts.Text      = lblTargetLifts.Text
    lblNewReps.Text      = lblTargetReps.Text

    Return p
End Sub

Public Sub setcvLogList(tabNo As Int)
    Dim cursor As Int
    Dim pcv As Panel
    Dim exerciseList As List = dbRT.getExerciseListByWGRec(tabNo + 1)
    setTableExRecsFromDb(exerciseList, 1)

    'Clear the custom view list
    If cvLogList.GetSize > 0 Then
    End If   
    cursor = 1
    For Each row()  As Object In exerciseList
        pcv = CreateListItem(cvLogList.AsView.Width, 90dip, row)
        cvLogList.Add(pcv, 90dip, 1)
        cursor = cursor + 1

End Sub

Code to set a label in an item

Sub whlWeight_tick
    tableLogDirtyFlag = True
    If tblLogDirty.weightKg = False Then
        tblLogUpdate.weightKg = tblLog.weightKg
    End If
    tblLogDirty.weightKg =  updateFromWheel(whlWeight, lblNewWeight, lblTargetWeight, 1, "", "")
    ToolBar.Menu.FindItem(20).Visible = Not(logDirtyCheck)
End Sub

Sub updateFromWheel(whl As WheelView, fieldToUpdate As Label, labelToCompare As Label, valuePos As Int, Prefix As String, Suffix As String) As Boolean
    'Put the selected weight in the New Weight Field and database mem record
    Dim whlStr As String
    Dim whlStrNum As String
    Dim lblCompStr As String
    Dim sf As StringFunctions2
    whlStr = whl.ReadWheel
    lblCompStr = sf.SplitGetWord(labelToCompare.Text, " ", 0)
    lblCompStr = lblCompStr.Trim
    whlStrNum  = sf.SplitGetWord(whlStr, " ", valuePos)
    whlStrNum  = whlStrNum.Trim
    If Prefix = "" And Suffix = "" Then
        fieldToUpdate.Text = whlStrNum & " " & sf.SplitGetWord(whlStr, " ", 2)
        fieldToUpdate.Text = Prefix & " " & whlStrNum & " " & Suffix
    End If       

    If lblCompStr.CompareTo(whlStrNum) = 0 Then
        fieldToUpdate.TextColor = 0xFF000000
        Return False
        fieldToUpdate.TextColor = 0xFFFF0000
        Return True
    End If
End Sub

I have tried to do this by changing the label text, but even though I jump to the item in the list I wish to change, it is the last record in the list that gets changed not the one I have jumped to.

How can this be done. Is there a simple way or do I have to delete the item I wish to change, make a new one and insert the new item in the old ones place?

Best regards


    31.3 KB · Views: 68


Active Member
Licensed User
Hi Erel,

I didn't understand the answer you gave, but I found a relevant video tutorial on xCustomListView - cross platform CustomListView

Custom List View Video Tutorial

This helped a lot and I ended up with the following code:

#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
    Type ItemValue(btnStart As Button, btnStop As Button, lblResps As Label, lblTargetWeight As Label)

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private btnStart As Button
    Private btnStop As Button
    Private lblResps As Label
    Private lblTargetWeight As Label
    Private cvLogViewIndex As Int

    Private clv1 As CustomListView
    Private btnClear As B4XView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub clv1_ItemClick (Index As Int, Value As Object)
End Sub

Sub clv1_ReachEnd
End Sub

Sub btnStop_Click
End Sub

Sub btnStart_Click
End Sub

Sub CreateItem(iv As ItemValue, i As Int) As B4XView
    Dim pcv As B4XView = xui.CreatePanel("")
    pcv.SetLayoutAnimated(0, 0, 0, 100%x, 90dip)
    iv.btnStart = btnStart
    iv.btnStop     = btnStop

    iv.lblTargetWeight = lblTargetWeight
    iv.lblTargetWeight.Text = i & " kg"
    iv.lblTargetWeight.TextColor = 0xFFFF0000
    iv.lblResps = lblResps
    iv.lblResps.Text      = "Rps " & i
    iv.lblResps.TextColor = 0xFF2EBB00

    Return pcv
End Sub

Public Sub setclv1
    Dim i As Int
    'Clear the custom view list
    If clv1.Size > 0 Then
    End If

    For i = 1 To 3
        Dim iv As ItemValue
        clv1.Add(CreateItem(iv, i), iv)
    cvLogViewIndex = 0
End Sub

Sub btnClear_Click
    Dim index As Int
    index = 0
    Dim iv As ItemValue = clv1.GetValue(index)
    iv.lblResps.Text = "22 Rps"
    iv.lblResps.TextColor = 0xFF000000
    iv.lblTargetWeight.Text = "3.5 Kg"
    iv.lblTargetWeight.TextColor = 0xFF000000
End Sub

I was able to select the list row and mod it from the btnClear_Click sub.

Hope this helps someone else.

Best regards
Upvote 0