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

rgarnett1955

Active Member
Licensed User
Longtime User
Hi,

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.Initialize("")
    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.
    p.LoadLayout("logCell")
    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
        cvLogList.Clear
    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
    Next
    cvLogList.JumpToItem(0)

End Sub

Code to set a label in an item

B4X:
'======================================================================================================
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
    
    sf.Initialize
    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)
    Else
        fieldToUpdate.Text = Prefix & " " & whlStrNum & " " & Suffix
    End If       

    If lblCompStr.CompareTo(whlStrNum) = 0 Then
        fieldToUpdate.TextColor = 0xFF000000
        Return False
    Else
        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
Rob.
 

Attachments

  • ResistanceTrainerForForum.zip
    31.3 KB · Views: 157

rgarnett1955

Active Member
Licensed User
Longtime 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:

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    '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:
    Activity.LoadLayout("main")
    setclv1

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)
    pcv.LoadLayout("logCell")
    
    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
        clv1.Clear
    End If
    

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

Sub btnClear_Click
    Dim index As Int
    index = 0
    clv1.JumpToItem(index)
    
    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
Rob
 
Upvote 0
Top