Android Question ListView and CheckBoxes too..

Derek Jee

Active Member
Licensed User
Longtime User
Hello

I am very new to B4A and hope to get off to a flying start. The app I need to build is very list/checkbox driven and must be able to do the following. I need to create a list based on data in a local sqllite database (I can already do this thanks :)). I must be able to have 2 checkboxes in each listview item. I must be able to check a checkbox AND also be able to select the list item to navigate to another activity and NOT check the checkbox.. When a checkbox is checked I'd like to run a routine so would to run the checkedChanged event.. I am sure someone out there has done this and would appreciate it if there is an example or a few examples combined which will help..

Thank you so much..

Derek.
 

Derek Jee

Active Member
Licensed User
Longtime User
Thank you Erel.. I can't see the CustomListView class in my designer (or CheckList).. I have been looking over your community but until I purchase I cannot see the examples.

I see that I have to add 2 libs to my project and add a customview.. But when I try to change the customtype there is nothing in the dropdown..

Thank you,


Derek.
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Yes. For Your need you need a customlistview. But as You already noticed You Need to have a full Version to be able to download additional libraries here
 
Upvote 0

Derek Jee

Active Member
Licensed User
Longtime User
Thank you Manfred.. I am close to getting the decision made.. :) Thank you for commenting..

Derek.
 
Upvote 0

eps

Expert
Licensed User
Longtime User
Upvote 0

Derek Jee

Active Member
Licensed User
Longtime User
Thank you eps.. I have seen them and hopefully once I am a paid member I can peek further.. Thank you. The support on here is quite wonderful. I hope one day to do the same for others..

Derek.
 
Upvote 0

Derek Jee

Active Member
Licensed User
Longtime User
Hi Guys.. I have been playing about with the examples and all works well until I add a database to the equation.. I am looping through my database to show items in my list. When I view only 10 list it takes about 3 seconds to load the page. If I view 30 it takes about 6 seconds. If I leave out the redrawing of the checkboxes it only takes a second, less really.. Have I got things correct? Here is my code..
B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
DimSQL1AsSQLCipher
End Sub

Sub Globals
Dimclv1AsCustomListView
End Sub

Sub Activity_Create(FirstTime AsBoolean)
Activity.LoadLayout("Main")

'Second list is created programmatically.
'Create 20 items made of a label, button and checkbox.
clv1.Initialize(Me, "clv1")
Activity.AddView(clv1.AsView, 0, 5%x, 100%x, 50%y)
SQL1.Initialize(File.DirRootExternal, "1.db", True, "3u2344k", "")
Dim Cursor1 AsCursor
Cursor1 = SQL1.ExecQuery("SELECT * FROM Areas")' Where PropertyID = '" & Main.PropertyID & "'")
For i = 0To Cursor1.RowCount - 1
Cursor1.Position = i
clv1.Add(CreateListItem(Cursor1.GetString("AreaName"), Cursor1.GetString("AreaID"), clv1.AsView.Width, 9%x), 9%x, "")
Next
Cursor1.Close
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed AsBoolean)
End Sub

Sub clv1_ItemClick(Index AsInt, Value AsObject)
Log(Index & " = " & Value)
End Sub
Sub CreateListItem(Text AsString, Text2 AsString, Width AsInt, Height AsInt) AsPanel
Dim p AsPanel
p.Initialize("")
p.Color = Colors.Black
Dim chk AsCheckBox
chk.Initialize("chk")
chk.Color = Colors.White
SetCBDrawable(chk,Colors.DarkGray,2Dip,Colors.Blue,Chr(10004),Colors.LightGray,60Dip,2dip)
Dim chk2 AsCheckBox
chk2.Initialize("chk2")
chk2.Color = Colors.Yellow
chk2.Enabled = False
SetCBDrawable(chk2,Colors.DarkGray,2Dip,Colors.Blue,Chr(10004),Colors.LightGray,60Dip,2dip)
Dim lbl AsLabel
lbl.Initialize("")
lbl.Gravity = Bit.OR(Gravity.TOP, Gravity.LEFT)
lbl.Text = Text
lbl.TextSize = 1.5%x
lbl.TextColor = Colors.White
p.AddView(lbl, 5dip, 2dip, 70%x, 50dip) 'view #0
p.AddView(chk, 71%x, 2dip, 60dip, 60dip) 'view #2
p.AddView(chk2, 85%x, 2dip, 60dip, 60dip) 'view #3
Return p
End Sub

Sub chk_CheckedChange(Checked AsBoolean)
Dim index AsInt
index = clv1.GetItemFromView(Sender)
Dim pnl AsPanel
pnl = clv1.GetPanel(index)
Dim chk AsCheckBox
chk = pnl.GetView(1)
Msgbox("Item value: " & clv1.GetValue(index) & CRLF & "Check value: " & chk.Checked, "")
End Sub

Sub chk2_CheckedChange(Checked AsBoolean)
Dim index AsInt
index = clv1.GetItemFromView(Sender)
Dim pnl AsPanel
pnl = clv1.GetPanel(index)
Dim chk2 AsCheckBox
chk2 = pnl.GetView(2)
Msgbox("Item value: " & clv1.GetValue(index) & CRLF & "Check value: " & chk2.Checked, "")
End Sub

Sub SetCBDrawable(CB AsCheckBox,BoxColor AsInt,BoxWidth AsInt,TickColor AsInt,TickChar AsString,DisabledColor AsInt,Size AsInt,Padding AsInt)
Dim SLD AsStateListDrawable
SLD.Initialize
Dim BMEnabled,BMChecked,BMDisabled AsBitmap
BMEnabled.InitializeMutable(Size,Size)
BMChecked.InitializeMutable(Size,Size)
BMDisabled.InitializeMutable(Size,Size)
'Draw Enabled State
Dim CNV AsCanvas
CNV.Initialize2(BMEnabled)
Dim Rect1 AsRect
Rect1.Initialize(Padding ,Padding ,Size - Padding ,Size - Padding)
CNV.DrawRect(Rect1,BoxColor,False,BoxWidth)
Dim Enabled,Checked,Disabled AsBitmapDrawable
Enabled.Initialize(BMEnabled)
'Draw Selected state
Dim CNV1 AsCanvas
CNV1.Initialize2(BMChecked)
If TickChar = "Fill"Then
CNV1.DrawRect(Rect1,TickColor,True,BoxWidth)
CNV1.DrawRect(Rect1,BoxColor,False,BoxWidth)
Else
CNV1.DrawRect(Rect1,BoxColor,False,BoxWidth)
'Start small and find the largest font that allows the tick to fit in the box
Dim FontSize AsInt = 6
DoWhile CNV.MeasureStringHeight(TickChar,Typeface.DEFAULT,FontSize) < Size - (BoxWidth * 2) - (Padding * 2)
FontSize = FontSize + 1
Loop
FontSize = FontSize - 1
'Draw the TickChar centered in the box
CNV1.DrawText(TickChar,Size/2,(Size + CNV.MeasureStringHeight(TickChar,Typeface.DEFAULT,25))/2,Typeface.DEFAULT,FontSize,TickColor,"CENTER")
EndIf
Checked.Initialize(BMChecked)
'Draw disabled State
Dim CNV2 AsCanvas
CNV2.Initialize2(BMDisabled)
CNV2.DrawRect(Rect1,DisabledColor,True,BoxWidth)
CNV2.DrawRect(Rect1,BoxColor,False,BoxWidth)
Disabled.Initialize(BMDisabled)
'Add to the StateList Drawable
SLD.AddState(SLD.State_Disabled,Disabled)
SLD.AddState(SLD.State_Checked,Checked)
SLD.AddState(SLD.State_Enabled,Enabled)
SLD.AddCatchAllState(Enabled)
'Add SLD to the Checkbox
Dim JO AsJavaObject = CB
JO.RunMethod("setButtonDrawable",ArrayAsObject(SLD))
End Sub
 
Last edited:
Upvote 0

Derek Jee

Active Member
Licensed User
Longtime User
Hi Manfred..

I did see it looks a bit unreadable.. How do I tag this as code? I cannot see the option..

Kind regards,

Derek.
 
Upvote 0

Derek Jee

Active Member
Licensed User
Longtime User
Found it.. Sorry.. (The CODE tag, not my answer)
 
Last edited:
Upvote 0

Derek Jee

Active Member
Licensed User
Longtime User
Well for whatever reason.. the list is now behaving with no speed problems.. :)
 
Upvote 0
Top