Android Question Multiple CheckBox in CustomListView

Discussion in 'Android Questions' started by Johan Hormaza, May 5, 2019.

  1. Johan Hormaza

    Johan Hormaza Active Member Licensed User

    Hello!
    How can I configure multiple CheckBox in an xCLV, but only one is enabled in the list, for example:
    That if I have a CheckBox for each article, I can enable only one and that when I enable another; The one that was already changed to disabled.
    Only one enabled in the entire list until you choose another.
    Thank you!
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    Sub Activity_Create(FirstTime As Boolean)
       
    Activity.LoadLayout("1")
       
    For i = 1 To 100
           
    Dim p As B4XView = xui.CreatePanel("")
           p.SetLayoutAnimated(
    000, CustomListView1.AsView.Width, 50dip)
           
    Dim chk As CheckBox
           chk.Initialize(
    "chk")
           p.AddView(chk, 
    10dip10dip30dip30dip)
           CustomListView1.Add(p, 
    "")
       
    Next
    End Sub

    Sub chk_CheckedChange(Checked As Boolean)
       
    If Checked Then
           
    Dim ItemIndex As Int = CustomListView1.GetItemFromView(Sender)
           
    For i = 0 To CustomListView1.Size - 1
               
    Dim p As B4XView = CustomListView1.GetPanel(i)
               
    If i <> ItemIndex Then
                   p.GetView(
    0).Checked = False
               
    End If
           
    Next
       
    End If
    End Sub
     
    mangojack likes this.
  3. udg

    udg Expert Licensed User

    Or..if you really have a large number of items, just keep a Global var as the index of the currently checked item and in the CheckedChange event pick it up, set its state to False and store the new index as the currently selected one.
    As alwyas there are pros and cons.
     
    mangojack likes this.
  4. Johan Hormaza

    Johan Hormaza Active Member Licensed User

    Thank you very much, you are the best!
     
  5. Johan Hormaza

    Johan Hormaza Active Member Licensed User

    When I add a design to Custom ListView I have this problem. What can be?

    main_chk_checkedchange (java line: 506)
    java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.CompoundButton
    at anywheresoftware.b4a.objects.B4XViewWrapper.setChecked(B4XViewWrapper.java:270)
    at b4a.example.checkbox.main._chk_checkedchange(main.java:506)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)
    at anywheresoftware.b4a.objects.CompoundButtonWrapper$1.onCheckedChanged(CompoundButtonWrapper.java:44)
    at android.widget.CompoundButton.setChecked(CompoundButton.java:156)
    at android.widget.CompoundButton.toggle(CompoundButton.java:115)
    at android.widget.CompoundButton.performClick(CompoundButton.java:120)
    at android.view.View$PerformClick.run(View.java:21163)
    at android.os.Handler.handleCallback(Handler.java:746)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5443)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    ** Activity (main) Resume **
     
  6. Johan Hormaza

    Johan Hormaza Active Member Licensed User

    The problem occurs when I'm using it with the floating title of xCustomListView
     
  7. Johan Hormaza

    Johan Hormaza Active Member Licensed User

    Ok, problem solved.
    I did it this way and it worked!
    Code:
    If Checked Then
                
    Dim ItemIndex As Int = CustomListView1.GetItemFromView(Sender)
                
    For i = 0 To CustomListView1.Size - 1
                    
    Dim p As B4XView = CustomListView1.GetPanel(i)
                    
    If i <> ItemIndex Then
                        
    For Each v As View In p.GetAllViewsRecursive
                            
    If v Is CheckBox Then
                                p.GetView(
    0).Checked = False
                            
    End If
                        
    Next
                    
    End If
                
    Next
            
    End If
     
  8. mangojack

    mangojack Well-Known Member Licensed User

    If you already know the Checkbox is the first view (0) on the Item / row panel ... there is now need to iterate thru all panel views. See @Erel 's code above.
    Also , I would only declare a panel if the Index did not equal ItemIndex.

    Code:
    If Checked Then
      
    Dim ItemIndex As Int = CustomListView1.GetItemFromView(Sender)
      
    For i = 0 To CustomListView1.Size - 1
        
    If  i <> ItemIndex Then      
          
    Dim p As B4XView = CustomListView1.GetPanel(i)
          p.GetView(
    0).Checked = False
        
    End If
      
    Next
    End If
     
    Johan Hormaza likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice