Android Tutorial List with two columns and a checkbox

Hello,

I have a TabHost which holds 4 tabs.
I want to have a list with two columns and a checkbox for each line in the list on the forth tab of the TabHost.

How can I do it? In case it's not possible - how can I do it without the checkboxes?

Thank you for your help,
Elad.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is not supported by ListView.
For a simple list with checkboxes you can use InputMultiList dialog.
However in your case I recommend you to use a ScrollView.
Here is an example of a list with checkboxes:
B4X:
Sub Process_Globals
End Sub
Sub Globals
    Dim ScrollView1 As ScrollView
    Dim lstChecks As List
    Dim height As Int
    height = 50dip
End Sub
Sub Activity_Create(FirstTime As Boolean)
    ScrollView1.Initialize(0)
    Dim pnl As Panel
    pnl = ScrollView1.Panel
    Activity.AddView(ScrollView1, 0, 0, 100%x, 100%y)
    lstChecks.Initialize
    
    For i = 1 To 100
        Dim chk As CheckBox
        chk.Initialize("")
        chk.Text = "Item #" & i
        lstChecks.Add(chk)
        Dim lbl1 As Label
        lbl1.Initialize("")
        lbl1.Text = "Value #" & i
        lbl1.Gravity = Gravity.CENTER_VERTICAL
        pnl.AddView(chk, 0, height * (i - 1), 120dip, height)
        pnl.AddView(lbl1, 125dip, height * (i - 1), 120dip, height)
    Next
    pnl.Height = lstChecks.Size * height
    Activity.AddMenuItem("Display checked", "mnuChecked")
End Sub

Sub mnuChecked_Click
    Dim sb As StringBuilder
    sb.Initialize
    For i = 0 To lstChecks.Size - 1
        Dim chk As CheckBox
        chk = lstChecks.Get(i)
        If chk.Checked Then
            sb.Append(i).Append(CRLF)
        End If
    Next
    Msgbox(sb.ToString, "Checked indices")
End Sub

20110106_01.png
 

Smee

Well-Known Member
Licensed User
Longtime User
I have adapted the code but it does not work correctly. Can anyone see where my error is.
The log shows the directories ok but they are not all added to the checkbox listing

B4X:
Sub Activity_Create(FirstTime As Boolean)
    ScrollView1.Initialize(0)
    Dim pnl As Panel
    pnl = ScrollView1.Panel
    Activity.AddView(ScrollView1, 0, 0, 100%x, 100%y)
    lstChecks.Initialize
    
   Dim datalist As List
datalist.Initialize
datalist = File.ListFiles(File.DirInternal & "/cats/")
datalist.SortCaseInsensitive(True)

For i = 0 To datalist.Size-1
If File.IsDirectory(File.DirInternal & "/cats/" ,datalist.Get(i)) = True Then
   Dim chk As CheckBox
    chk.Initialize("")
    chk.Text = datalist.Get(i)
   Log(datalist.Get(i))
    lstChecks.Add(chk)
    pnl.AddView(chk, 0, height * (i - 1), 200dip, height)
End If
Next

pnl.Height = lstChecks.Size * height
Activity.AddMenuItem("Display checked", "mnuChecked")
 

klaus

Expert
Licensed User
Longtime User
Which one are you missing, the first one ?
At least this line
B4X:
pnl.AddView(chk, 0, height * (i - 1), 200dip, height)
should be
B4X:
pnl.AddView(chk, 0, height * i, 200dip, height)
In Erel's code i begins with 1.
In your code i begins with 0.
So, with your code, you are not seeing the first checkbox.

Best regards.
 

Smee

Well-Known Member
Licensed User
Longtime User
Thanks for the very quick reply Klaus,

I was missing the first one but also the list only show the first 10 directories and there is a gap between the fifth and sixth directory as if something should be there


UPDATE:
i changed this line to read

pnl.Height = lstChecks.Size * height * 2

which fixes the problem but i do not uderstand why and also there is a gap after every fifth line
 
Last edited:

klaus

Expert
Licensed User
Longtime User
You should also add a counter variable for the added directories, like
B4X:
j = 0
For i = 0 To datalist.Size-1 
   If File.IsDirectory(File.DirInternal & "/cats/" ,datalist.Get(i)) = True Then     
     Dim chk As CheckBox 
     chk.Initialize("") 
     chk.Text = datalist.Get(i)     
     Log(datalist.Get(i))     
     lstChecks.Add(chk)     
     pnl.AddView(chk, 0, height * j, 200dip, height) 
     j = j + 1
   End If 
Next
pnl.Height = j * height
Best regards.
 
Last edited:

Smee

Well-Known Member
Licensed User
Longtime User
With the counter i am missing directories:BangHead::BangHead:

pnl.Height = j * height

With this line

pnl.Height = lstChecks.Size * height * 2

i ghet all the directories but i still have a gap evey fifth line :confused:
 

klaus

Expert
Licensed User
Longtime User
Strange !

Do have a screenshot that shows the problem with a copy of the Log Tab where you display the directories.
Or could you post your project so we could try to reproduce the problem in exactly the same conditions as yours.

Best regards.
 

Smee

Well-Known Member
Licensed User
Longtime User
Thanks for your help Klaus,

I tried to take a screenshot but i get an error

"cannot take screenshot from device connected with B4A-Bridge."

the project is too big to post.
I will play around and see what happens. Fortunately this view and code is for my own use so it is not critical but i would like to know what is going on

cheers

Joe
 

Smee

Well-Known Member
Licensed User
Longtime User
An updater to this problem
i added a log to the check height and directory name and a strange thing is happening
Where the gaps are (after the fifth directory the gap is not 140 but 210
:confused:


0 baby
140 bagswallets
280 bathbody
420 bathroom
560 beauty

770 bodytools

910 eyelipdental
1050 firstaid
1190 generalgitfs

1330 generalsundries

1540 haircare
1680 jewellery
1820 kids
1960 photoframes
2100 plushtoys
2310 seasonal
2450 specials
2590 summerswim
2730 sweets
2870 winter
3010 wrapping
** Activity (downloaddirectory) Resume **

i fixed the problem by adding the following

B4X:
    pnl.AddView(chk, 0, ht , 200dip, height)
   ht=ht+140
....
....
....
End If
Next

pnl.Height = ht

but it is still strange
 

klaus

Expert
Licensed User
Longtime User
There must still something be wrong.

Could you pot a Log just after For...:
B4X:
For i = 0 To datalist.Size-1 
   Log(datalist.Get(i))
   If File.IsDirectory(File.DirInternal & "/cats/" ,datalist.Get(i)) = True Then
to see if the content of datalist is really correct.

Best regards.
 

malci

Member
Licensed User
Longtime User
Sorry, I borrowed this topic, but ... I have a problem with list too.

how should i do "Item_click" on my List (not ListView) or "..._touch"


B4X:
 Sub Globals

Dim ScrollView1 As ScrollView
   Dim pnlHeader As Panel ' panel za glavo
   Dim pnlTop As Panel ' panel za gumbe
   Dim pnlGlava As Panel ' panel za Glavo naročila
   Dim pnlNarocilo As Panel ' panel za seznam materiala
   Dim lstNarocilo As List

End Sub

Sub Activity_Create(FirstTime As Boolean)

         
   ScrollView1.Initialize(0)
   scrollview1.Color=Colors.LightGray
   pnlNarocilo=ScrollView1.Panel
   pnlNarocilo.Color=Colors.Cyan
   Activity.AddView(ScrollView1, 0dip, 27%y, 100%x, 84%y)
   
   lstNarocilo.Initialize

        pnlTop.Initialize("pnlTop")
   activity.AddView(pnlTop, 0dip,0dip,activity.Width ,15%y)
   pnlTop.Color=Colors.Yellow
   
        pnlHeader.Initialize("pnlHeader")
   pnlHeader.Color = Colors.Blue
   Activity.AddView(pnlHeader,0dip,16%y,activity.Width,10%y)

End Sub      

Sub btnNew_Click

   Dim vrsta_1 As Int
   Dim vrsta  As Int

   vrsta_1=pnlTop.Height + pnlHeader.Height
   vrsta= ((lstNarocilo.Size*RowHeight)/3)+vrsta_1
      
    Dim lblNo As Label
        lblNo.Initialize("")
        lblNo.Text =  i
   lblNo.TextColor=Colors.Black
        lblNo.Gravity = Gravity.CENTER_HORIZONTAL   
      Log("lblNo: " & lblNo)
   lstNarocilo.Add(lblNo)
      
   Dim lblNaziv As Label
        lblNaziv.Initialize("")
        lblNaziv.Text = "Moj Material: " & i
   lblNaziv.TextColor=Colors.Black
        lblNaziv.Gravity = Gravity.LEFT
      
      Log ("lblNaziv " & lblNaziv)
      lstNarocilo.Add(lblNaziv)
      
Dim lblKolicina As Label
      lblKolicina.Initialize("")
      lblKolicina.Text="23"
      lblKolicina.TextColor=Colors.Black
      lblKolicina.TextSize=15
      lblNaziv.Gravity = Gravity.LEFT
      
      lstNarocilo.Add(lblKolicina)            
      'vrsta=lstChecks.Size*height
        
                pnlNarocilo.AddView(lblNo, 0dip, vrsta, 20%x, RowHeight)
      pnlNarocilo.AddView(lblNaziv, 20%x, vrsta, 60%x, RowHeight)
      pnlNarocilo.AddView(lblKolicina,80%x,vrsta,20%x,RowHeight)
      
      pnlNarocilo.Height = (lstNarocilo.Size * RowHeight)/3

end sub


This allows us to do in "table" based on "Scrollview".

by

uros
 

StarinschiAndrei

Active Member
Licensed User
Longtime User
how can i read editText from scrolview

Hi ,
Can anybody help me?
I changed your exemple , i added a text box in my app. now if the chk is checked i want to read the textbox (attached a printsceern, the value that i want to read is "bla bla").
Here is my code :
...............
For i=0 To cur.RowCount-1
cur.Position =i

Dim chk As CheckBox
chk.Initialize("chk")
chk.Gravity =Gravity.CENTER
chk.TextSize =18
chk.TextColor =culoaretext
chk.Text =cur.Getstring("nume")&" "&cur.GetString("prenume")
chk.Tag =cur.GetInt("id")
lstChecks.Add(chk)
Dim lbl As Label
lbl.Initialize("lbl")
lbl.Gravity =Gravity.CENTER
lbl.TextSize =18
lbl.TextColor =culoaretext
lbl.Text =cur.GetString("nrOre")
Dim txt As EditText
txt.Initialize("txt")
txt.TextSize=18
txt.TextColor=culoaretext
txt.Text =""
svprezenta.Panel.AddView(chk, 0, height * (i - 1), 33%x, height)
svprezenta.Panel.AddView(lbl, 33%x, height * (i - 1), 33%x, height)
svprezenta.Panel.AddView(txt,66%x,height*(i-1),33%x,height)
''svprezenta.Panel.AddView(chk,colwidth*i,rowhight*y,colwidth,rowhight)

Next
...................

Sub btnvalidare_Click
For i = 0 To lstChecks.Size - 1
Dim chk As CheckBox
Dim txt As EditText
chk = lstChecks.Get(i)
If chk.Checked Then
txt=svprezenta.Panel.GetView(i)
'txt.text is empty
Msgbox(chk.Tag&txt.Text ,"")

End If
Next

End Sub
 

Attachments

  • Screenshot.png
    Screenshot.png
    78.3 KB · Views: 1,165
Last edited:

klaus

Expert
Licensed User
Longtime User
Try with this code:
B4X:
If chk.Checked Then
   txt=svprezenta.Panel.GetView(i * 3 + 2) 
   Msgbox(chk.Tag&txt.Text ,"")
End If
Indexes of the views in the ScrollView:
chk 0 -> 0 * 3
lbl 1 -> 0 * 3 + 1
txt 2 -> 0 * 3 + 2

chk 3 -> 1 * 3
lbl 4 -> 1 * 3 + 1
txt 5 -> 1 * 3 + 2

chk 6 -> 2 * 3
lbl 7 -> 2 * 3 + 1
txt 8 -> 2 * 3 + 2

etc.

Best regards.
 

StarinschiAndrei

Active Member
Licensed User
Longtime User
Try with this code:
B4X:
If chk.Checked Then
   txt=svprezenta.Panel.GetView(i * 3 + 2) 
   Msgbox(chk.Tag&txt.Text ,"")
End If
Indexes of the views in the ScrollView:
chk 0 -> 0 * 3
lbl 1 -> 0 * 3 + 1
txt 2 -> 0 * 3 + 2

chk 3 -> 1 * 3
lbl 4 -> 1 * 3 + 1
txt 5 -> 1 * 3 + 2

chk 6 -> 2 * 3
lbl 7 -> 2 * 3 + 1
txt 8 -> 2 * 3 + 2

etc.

Best regards.

Thank you Klaus :) it works fine
 

Dman

Active Member
Licensed User
Longtime User
This is not supported by ListView.
For a simple list with checkboxes you can use InputMultiList dialog.
However in your case I recommend you to use a ScrollView.
Here is an example of a list with checkboxes:
B4X:
Sub Process_Globals
End Sub
Sub Globals
    Dim ScrollView1 As ScrollView
    Dim lstChecks As List
    Dim height As Int
    height = 50dip
End Sub
Sub Activity_Create(FirstTime As Boolean)
    ScrollView1.Initialize(0)
    Dim pnl As Panel
    pnl = ScrollView1.Panel
    Activity.AddView(ScrollView1, 0, 0, 100%x, 100%y)
    lstChecks.Initialize
    
    For i = 1 To 100
        Dim chk As CheckBox
        chk.Initialize("")
        chk.Text = "Item #" & i
        lstChecks.Add(chk)
        Dim lbl1 As Label
        lbl1.Initialize("")
        lbl1.Text = "Value #" & i
        lbl1.Gravity = Gravity.CENTER_VERTICAL
        pnl.AddView(chk, 0, height * (i - 1), 120dip, height)
        pnl.AddView(lbl1, 125dip, height * (i - 1), 120dip, height)
    Next
    pnl.Height = lstChecks.Size * height
    Activity.AddMenuItem("Display checked", "mnuChecked")
End Sub

Sub mnuChecked_Click
    Dim sb As StringBuilder
    sb.Initialize
    For i = 0 To lstChecks.Size - 1
        Dim chk As CheckBox
        chk = lstChecks.Get(i)
        If chk.Checked Then
            sb.Append(i).Append(CRLF)
        End If
    Next
    Msgbox(sb.ToString, "Checked indices")
End Sub

I am trying to adapt this for my project. What I have is basically the same except for the lbl1.text is a random number. I would like to have the Activity.Title contain the sum of the lbl1.text that corresponds to the appropriate checked boxes without having to click on the menu button. In other words is it possible to have the title update the totals just by checking and unchecking the check boxes?
 
Last edited:
Top