Android Question (Solved) B4XRadioButton inside xCustomListView

aeric

Expert
Licensed User
Longtime User
How do I capture the index for B4XRadioButton_Checked inside xCustomListView?
I got an error:
java.lang.ClassCastException: b4j.example.b4xradiobutton cannot be cast to javafx.scene.Node
B4X:
Private Sub B4XRadioButton3_Checked
    Dim Index As Int = CLV.GetItemFromView(Sender)
    Dim pnl As B4XView = CLV.GetPanel(Index)
    Dim Sel As B4XView = pnl.GetView(0)
    For Each v As B4XView In CLV.AsView.GetAllViewsRecursive
        If v Is B4XRadioButton And v.Tag <> Sel.Tag Then
            v.Checked = False
        End If
    Next
    Log(pnl)
    Log(Index)
End Sub

Sample project attached.

Edit: Sorry, I wrongly upload B4J project. Please move to B4J forum. Thanks.
 

Attachments

  • B4XRadioButton.zip
    4.7 KB · Views: 180

mangojack

Expert
Licensed User
Longtime User
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I understand how to get the index of a panel but I don't understand how to get the Tag of a B4XRadioButton when clicked.
What I want to achieve is to uncheck all the B4XRadioButtons inside a CustomListView except the one I just selected.
This is easy if the B4XRadioButtons are on the same pane/panel.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    B4XPages.SetTitle(Me, "B4XRadioButton Example")
    For i = 0 To 5
        CLV.Add(CreateItem (i), i)
    Next
End Sub

Sub CreateItem (Index As Int) As B4XView
    Dim P As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0, 0, 0, CLV.AsView.Width, 160dip)
    p.LoadLayout("Item")
    B4XRadioButton3.Tag = Index ' <-- I need to assign value to Tag
    Label1.Text = "Item " & (Index + 1)
    Return P
End Sub

Private Sub B4XRadioButton3_Checked
    Dim rb As B4XRadioButton = Sender
    For Each v As B4XView In CLV.AsView.GetAllViewsRecursive
        If v.Tag Is B4XRadioButton Then
            Dim s As B4XRadioButton = v.Tag
            Log("s.Tag=" & s.Tag & " | rb.Tag=" & rb.Tag)
            If s.Tag <> rb.Tag Then
                s.Checked = False
            End If
        End If
    Next
    Log(rb.Tag)
End Sub
 

Attachments

  • B4XRadioButton2.zip
    4.9 KB · Views: 195
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Do you mind to share your code (without setting value to Tag)?
Here is my code without having to assign tags to B4XRadioButton3
B4X:
Private Sub B4XRadioButton3_Checked 
    Dim r As B4XRadioButton = Sender        
    'Below code, iterate thru all X4XRadio buttons and uncheck all in clv except the one that is checked:
    Dim ItemIndex As Int = CLV.GetItemFromView(r.mBase)
    For i = 0 To CLV.Size - 1
        Dim p As B4XView = CLV.GetPanel(i)
        If i <> ItemIndex  Then  
            Dim Sel As B4XView = p.GetView(0)
            For Each vv As B4XView In Sel.GetAllViewsRecursive
                If vv.Tag Is B4XRadioButton Then
                    Dim v As B4XRadioButton =vv.Tag
                    v.Checked = False
                End If
            Next
        End If
    Next    
End Sub
You can eliminate this line from your CreateItem sub:
B4XRadioButton3.Tag = Index
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Here is my code without having to assign tags to B4XRadioButton3
B4X:
Private Sub B4XRadioButton3_Checked
    Dim r As B4XRadioButton = Sender       
    'Below code, iterate thru all X4XRadio buttons and uncheck all in clv except the one that is checked:
    Dim ItemIndex As Int = CLV.GetItemFromView(r.mBase)
    For i = 0 To CLV.Size - 1
        Dim p As B4XView = CLV.GetPanel(i)
        If i <> ItemIndex  Then 
            Dim Sel As B4XView = p.GetView(0)
            For Each vv As B4XView In Sel.GetAllViewsRecursive
                If vv.Tag Is B4XRadioButton Then
                    Dim v As B4XRadioButton =vv.Tag
                    v.Checked = False
                End If
            Next
        End If
    Next   
End Sub
You can eliminate this line from your CreateItem sub:
B4XRadioButton3.Tag = Index
I also want return the value of selected B4XRadioButton.
 
Upvote 0
Top