Android Question wrong tag in the CheckBoxes

mevial

Member
Licensed User
Longtime User
While I checked RadioButton, sender tag is correct, but while I checked CheckBox, sender tag always shows last checkbox.

checkbox test:
Sub Process_Globals
End Sub

Sub Globals
    Dim RBtn As RadioButton
    Dim ChBox As CheckBox
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim i As Int
    Dim YPos As Double=0
    For i=0 To 2
        RBtn.Initialize("RBtn")
        RBtn.Tag=i
        RBtn.Text="Button " & i
        Activity.AddView(RBtn,0,YPos,100%x,40dip)
        YPos=YPos+40dip
    Next
    For i=100 To 102
        ChBox.Initialize("ChBox")
        ChBox.Tag=i
        ChBox.Text="CheckBox " & i
        Activity.AddView(ChBox,0,YPos,100%x,40dip)
        YPos=YPos+40dip
    Next
    
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Private Sub RBtn_CheckedChange(Checked As Boolean)
    Dim r As View
    r=Sender
    ToastMessageShow(r.Tag,False)
End Sub

Private Sub ChBox_CheckedChange(Checked As Boolean)
    Dim r As View
    r=Sender
    ToastMessageShow(r.Tag,False)
End Sub
 

Mahares

Expert
Licensed User
Longtime User
checked CheckBox, sender tag always shows last checkbox.
You were using the same view over and over. You need the dim inside the loop like this:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim i As Int
    Dim YPos As Double=0
    For i=0 To 2
        Dim RBtn As RadioButton
        RBtn.Initialize("RBtn")
        RBtn.Tag=i
        RBtn.Text="Button " & i
        Activity.AddView(RBtn,0,YPos,100%x,40dip)
        YPos=YPos+40dip
    Next
    For i=100 To 102
        Dim ChBox As CheckBox
        ChBox.Initialize("ChBox")
        ChBox.Tag=i
        ChBox.Text="CheckBox " & i
        Activity.AddView(ChBox,0,YPos,100%x,40dip)
        YPos=YPos+40dip
    Next   
End Sub
 
Upvote 2
Top