Android Question Trying to update buttons on a panel but only first button being updated

Robert S

Member
Licensed User
Longtime User
I have 8 buttons on a panel and I want to set the tag and text value at runtime based on values in contacts. I am getting the contact data OK but when I try and update the buttons with it only the first button is updated rather than each button in turn being updated. I thought the value v would be set to each button in turn but it does not appear to be doing so. Code below.

counter = 0 'contact counter
For Each v As View In pnlButtons.GetAllViewsRecursive
Private c As CustomContact
c.Initialize
c = contactsList.Get(counter)
If c.DisplayName = "" Or c.phoneNumber = "" Then
Do While c.DisplayName = "" Or c.phoneNumber = ""
'stay on same button and go to next contact
counter = counter + 1
c = contactsList.Get(counter)
Loop
Else
counter = counter + 1
End If
If counter >= numButtons Then
Exit
Else
updateButton(v, c)
End If
Next
End If
End Sub

Private Sub updateButton(b As B4XView, c As CustomContact)
b.Text = c.DisplayName
b.Tag = c.phoneNumber
b.Enabled = True
b.Visible = True
 

Brian Dean

Well-Known Member
Licensed User
Longtime User
Your code seems far too complicated for the simple task that you describe. Assuming that all the views in pnlButtons are buttons -

B4X:
Sub Test
    Private c As CustomContact
    c.Initialize
    Dim counter As Int = 0
    For Each b As Button In pnlButtons.GetAllViewsRecursive
        If (counter < contactsList.Size) Then
            c = contactsList.Get(counter)
            b.Text = c.DisplayName
            b.Tag = c.phoneNumber
            b.Enabled = True
            b.Visible = True
            counter = counter + 1
        End If
    Next
End Sub

If the panel contains views that are not buttons then iterate through all views but check for buttons . . .
B4X:
    For Each v As View In pnlButtons.GetAllViewsRecursive
        If (v Is Button) Then
            If (counter < contactsList.Size) Then
                ....
                ....  
            End If
        End If
    Next

If some of your buttons are already filled and you want to skip over them, then skip . . .
B4X:
    For Each b As Button In pnlButtons.GetAllViewsRecursive
        If (b.Text.Length = 0) Then
            If (counter < contactsList.Size) Then
                c = contactsList.Get(counter)
                ....

By the way, when you want to include a code fragment in a post use the </> option at the top left of the reply window.
 
Last edited:
Upvote 0

Robert S

Member
Licensed User
Longtime User
I tried that and various permutations. I also tried using a case statement to reference the declared button variables
Select counter
Case 1
btnContact1.Tag = c.phoneNumber
btnContact1.Text = c.DisplayName
Case 2
btnContact2.Tag = c.phoneNumber
btnContact2.Text = c.DisplayName
Watching the button variable values during execution they get updated correctly but still only the first button is displayed and it has the values of the last contact in the list.
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
I tried that and various permutations.
Well, it works for me, unless there are some other constraints that you are not telling me about. I am a bit pushed for time now and this will be my last post, but here is a working demonstration.
 

Attachments

  • Test.zip
    9.4 KB · Views: 61
Upvote 0

Robert S

Member
Licensed User
Longtime User
Thanks, I appreciate the help. I need to test, but the immediate difference is I have coded in B4XMainPage and you have populated the buttons from a button click event whilst I have used a sub call from a B4XPage event (I tried 'Create' and 'Appear'). I will try a screen button to populate.
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
I have used a sub call from a B4XPage event (I tried 'Create' and 'Appear').
Yes - maybe that is where the problem lies. Here is an updated example that tests for empty buttons. Now I really must go - meal is on the table.
 

Attachments

  • Test2.zip
    9.4 KB · Views: 58
Upvote 0

Robert S

Member
Licensed User
Longtime User
Found the issue: the layout had got corrupted somehow. When I recreated it from scratch the code worked as expected (well in B4A, I will need to try and port it to B4X at some point).
 
Upvote 0
Top