B4A Library [Class] CheckList

Hi,

I created a class to manage a list with checkboxes (but not only, you can use it for any kind of list with rows). The layout is highly customizable and you can even extend a clicked item with extra content. You can rearrange the items programmatically or manually (drag & drop).

Tutorials: How they do #1, How they do #3

img1.jpg img2.jpg img3.jpg img4.jpg img5.jpg

v1.1: I added the ExtendItem function and an option to check the box when items are added

v1.2: I fixed the width problem (in a container like TabHost, the width property returns -1, so we have to use a different method to get the SV width) and, in the demo, a click on the extended item collapses now its content.

v1.3: I added a new function to add items without checkbox (AddItemNoChkbx) so you can use this class as a ListView replacement;
I changed the way it colors the selected item (now it complies to the default Android theme);
I added an option to color differently the extended item.

v1.4: I optimized the code a little bit and I added three functions:
AddCustomItem (adds your own layout to the list);
AdjustBitmap (resizes the bitmap to fit the given dimensions);
GetFirstText (returns the first label text found for the given ID).

v1.5: I simplified and improved the management of extended items (I also fixed a bug). Unfortunately, that could break existing code.
Inspired by the class CustomListView, I added a parameter to Initialize: DividerHeight (if you want to put a colored divider between items) and several functions:
- NumberOfItems
- getHeight
- JumpToPosition
- JumpToItem
- CollapseItem
- HasExtraContent
- ExtendedItemID

v1.6:
Now, you can define the color of checked items and the background color (transparent by default).
I added two (very fast) functions:
- JumpToView (scrolls the list to the given view)
- FindPanelContaining (returns the panel containing the given view)
I added another example.

v2.0:
I added more than 10 new functions (MoveItem, RemoveItemAt, SwapItems, SortItems, ReplaceItem, ApplyColors, CreateTextList, etc.). See post #21 for complete list;
I changed the ID type (formerly Int) to Object (that shouldn't break the existing code);
I added the Drag & Drop feature;
I optimized JumpToPosition (a lot quicker now);
I fixed a bug with colors.

v2.1:
I added functions to filter the list;
I added CreateIntegerList to ease the sorting and filtering of integer values;
Now you can change the item height in ReplaceItem.

v2.2:
I fixed a bug (BackgroundColor was not applied to new items)
I added new functions:
- FindPanelWithID(ID As Object) As Object
- SetOnPaintListener(SubName As String)
- Repaint(pnlItem As Panel)
I renamed ApplyColors to RepaintAllItems

v2.21:
I changed an incorrect declaration in the demo #1 and #2.
The class is now compatible with the obfuscation option.

v2.22:
The class no longer displays warnings with B4A v2.7x.
All examples are now compatible with the obfuscation option.

Complete list of functions here

Fred

PS: you need the Reflection Lib
 

Attachments

  • CheckList v2.22.zip
    60.2 KB · Views: 2,362
Last edited:

Informatix

Expert
Licensed User
Longtime User
Any idea how to change the orange color that is describing the press action of items ?
Hope I made myself understood !

I did played with colors found as process variables with no luck.
Thank you
This is not a color in fact but a drawable. By default, the class gets the default drawable for pressed items of your device. You can change that in the class. Search PressedDrawable.
 

marcel

Active Member
Licensed User
Longtime User
Hi,

Would it be possible to have a tree layout? I have items and SubItems I would like to Check. Would this be possible? An Example would be create!
 

marcel

Active Member
Licensed User
Longtime User
Hi,

I have some issues with the class. I need to remove all the items which I do with the following function:

B4X:
Public Sub RemoveAllItems
    Do While NumberOfItems > 0
        RemoveItemAt(0)
    Loop
End Sub

But when I build up the list again I get an error in this function:
B4X:
Private Sub GetParentPanel(MyView As View) As View
    Dim r As Reflector
    Dim v, Parent As View
    r.Target = MyView
    v = r.Target
    Parent = r.RunMethod("getParent")
    Do While Parent <> sv.Panel
        r.Target = Parent
        v = r.Target
        Parent = r.RunMethod("getParent")
    Loop
    Return v
End Sub

this is the exception log:
B4X:
Did you forget to call Activity.LoadLayout?
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:49)
    at com.yazula.android.clschecklist._getparentpanel(clschecklist.java:1937)
    at com.yazula.android.clschecklist._findpanelcontaining(clschecklist.java:102)
    at com.yazula.android.clscategory._customchbx_checkedchange(clscategory.java:249)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:302)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
    at anywheresoftware.b4a.objects.CompoundButtonWrapper$1.onCheckedChanged(CompoundButtonWrapper.java:44)
    at android.widget.CompoundButton.setChecked(CompoundButton.java:137)
    at anywheresoftware.b4a.objects.CompoundButtonWrapper.setChecked(CompoundButtonWrapper.java:55)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:636)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:305)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
    at anywheresoftware.b4a.BA$2.run(BA.java:285)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4921)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
    at dalvik.system.NativeStart.main(Native Method)

Some how it removes a view that is not initialized again with building op the list again. This is my function that refreshes the layout:

B4X:
Public Sub RefreshLayout
 
  lst.RemoveAllItems
 
  Dim l As List
  l=Helpers.SelectCategories(Main.db)

  For Each item As Category In l
          Dim pnl As Panel, lbl As Label, cbx As CheckBox, tBtn As ToggleButton, w As Int
        pnl.Initialize("")
        lbl.Initialize("")
        cbx.Initialize("customChBx")
        cbx.Tag=item.id
        cbx.checked=item.Selected
        lbl.Text = item.Name
        lbl.TextSize = 13
        lbl.TextColor = Colors.DarkGray
        lbl.Gravity = Gravity.CENTER_VERTICAL
       
        Dim checked, unchecked As ColorDrawable
       
        checked.Initialize(Colors.LightGray,0dip)
        unchecked.Initialize(Colors.LightGray, 0dip)
       
        tBtn.Initialize("customTBtn")
        tBtn.TextOff="Overal"
        tBtn.TextOn="Favorite"
        tBtn.TextColor=Colors.RGB(236,106,30)
        tBtn.Tag=item.id
        tBtn.checked = item.Local
       
        Dim sld As StateListDrawable
        sld.Initialize
        sld.AddState(sld.State_Checked, checked)
        sld.AddState(sld.State_Unchecked, unchecked)

        tBtn.Background = sld

        pnl.AddView(cbx,0,0,40dip,40dip)
        w = lst.getWidth - 90dip
        pnl.AddView(lbl,45dip, 0, w, 40dip)   
        w = w + 10dip
        pnl.AddView(tBtn, w, 0, 80dip, 38dip)
        lst.AddCustomItem(item.id, pnl, 40dip)
  Next
  lst.ResizePanel
End Sub
 
Last edited:

PABLO2013

Well-Known Member
Licensed User
Longtime User
GREETINGS THANK YOU,
I have the following problem:

Sub CreateItem(h, i As Int) As Panel
.......

Dim pnl As Panel, lbl As Label, chk As CheckBox, edt As EditText, sb As StartPointSeekBar
pnl.Initialize("")
..........
sb.Initialize("sb") 'View #1
sb.TheBackgroundColor = Colors.Cyan
sb.TheRangeColor = 0xFF33B5E5
sb.MinValue = -9 'Keep the order of this 3 lines of code
sb.maxValue = 9
sb.ThumbValue = 1
sb.SetColorAnimated(300,2323,343345)
pnl.AddView(sb, 150, 15, 250dip, 50dip)
.....
Return pnl
End Sub

Sub sb_value_changed

Dim Checked As Boolean
Checked=False
Dim pnl As Panel
pnl = lst.FindPanelContaining(Sender) i can not find panel containing
...........

End Sub


One error occurs in the module: CheckList v2.22
could you tell me this because doing wrong
Thank you
 

Attachments

  • error.PNG
    error.PNG
    86.9 KB · Views: 252

Informatix

Expert
Licensed User
Longtime User
GREETINGS THANK YOU,
I have the following problem:

Sub CreateItem(h, i As Int) As Panel
.......

Dim pnl As Panel, lbl As Label, chk As CheckBox, edt As EditText, sb As StartPointSeekBar
pnl.Initialize("")
..........
sb.Initialize("sb") 'View #1
sb.TheBackgroundColor = Colors.Cyan
sb.TheRangeColor = 0xFF33B5E5
sb.MinValue = -9 'Keep the order of this 3 lines of code
sb.maxValue = 9
sb.ThumbValue = 1
sb.SetColorAnimated(300,2323,343345)
pnl.AddView(sb, 150, 15, 250dip, 50dip)
.....
Return pnl
End Sub

Sub sb_value_changed

Dim Checked As Boolean
Checked=False
Dim pnl As Panel
pnl = lst.FindPanelContaining(Sender) i can not find panel containing
...........

End Sub


One error occurs in the module: CheckList v2.22
could you tell me this because doing wrong
Thank you
A view is expected by the function but the sender object is not. Please see this problem with the library author because I have no idea of what is a "StartPointSeekBar".
 

Dave O

Well-Known Member
Licensed User
Longtime User
I'm using a checklist with a long click to activate drag and drop.

However, each item (panel) contains several labels, and if the user happens to press the label, the drag-and-drop is not started (presumably because the label itself is getting the long click, not the panel beneath it).

Ideally I would like the entire item to accept a long click to drag-and-drop. Is there a way to "pass through" the long click from the label to the panel? (I tried setting up a long-click event for the label, and using it to trigger the drag-and-drop (by calling the item's StartDragAndDrop sub, or by calling the item's LongClick sub itself), but neither worked.)

I could also just replace the Checklist class with a ULV, but I'm wondering if I'll still have the same issue? (In the end, I may just switch to ULV and use an on-touch button for drag-and-drop instead.)

Thanks!
 

Dave O

Well-Known Member
Licensed User
Longtime User
Yes, that was the problem. I had added click events to the labels, and that interfered with the long click. Fixed and working well again. Thanks!
 

PABLO2013

Well-Known Member
Licensed User
Longtime User
Greetings,
I've been reviewing your project and I feel very good ,
I only know how i can change the characteristics of a label view that does not necessarily have the focus.
Example: if I put a number between 1 and 9 in a text view ( example (5) ) and have 9 items (with more views), put the blue the text on a label to the item 5 ... , thanks
 
Top