Android Question Listview with headers?

U

unba1300

Guest
Hi. I'm using a listview to show a scrollable list of about 25 categories, but I'd like to group the categories into a few subjects and then have a header (title) for each subject. Any suggestions? Thanks.
 

mangojack

Expert
Licensed User
Longtime User
I do something similar displaying data from a DB. But it is displayed alphabetically and grouped accordingly .. (see attached image ...)
You might be able to adapt it to suit your needs. (Catagories)
B4X:
Sub PopulateItemList
  
   Dim oldChar As String        'current alpha
   Dim newChar As String       'new alpha  .. next product starting with new alpha
   Dim recData(3) As String     'product and size
  
   'fill Items list with records
   lvwItems.Clear

   Cursor1 = SQL1.ExecQuery("SELECT ID, Product, Size FROM "& StockTable &" ORDER BY Product Asc")
  
   numRec = Cursor1.RowCount
   lvwItems.Clear
  
   For  i = 0 To numRec -1
     Cursor1.Position = i
     recData(0) = Cursor1.GetInt("ID")
     recData(1) = Cursor1.GetString("Product")
     recData(2) = Cursor1.GetString("Size")
    
     'test for new Alpha  .. if so show new alpha prior to item list  eg: C  Cake
     newChar = recData(1).SubString2(0,1)
     If newChar <> oldChar Then
       lvwItems.AddSingleLine(newChar)
       oldChar = newChar    
     End If
            
     'using .AddTwoLines2 allowing me to specify return value for click events  ie.. items db ID and items list position (i) 0 start
     'this is regex'd on click to variables itemID & itemPos
     lvwItems.AddTwoLines2(recData(1) , recData(2 ),recData(0) & "," & i)    
   Next    
  
   Cursor1.Close
  
End Sub

Sub FormatLists

   'format listview Items
   lvwItems.Width = 500dip
  lvwItems.Height = 560dip  
   lvwItems.Color = 0xFF4682B4 'SteeleBlue
   lvwItems.ScrollingBackgroundColor = Colors.Transparent
  
   lvwItems.SingleLineLayout.ItemHeight = 24  
  
   lvwItems.SingleLineLayout.Label.Top = 0
   lvwItems.SingleLineLayout.Label.Left = 5dip  
   lvwItems.SingleLineLayout.Label.Height=24dip
   lvwItems.SingleLineLayout.Label.TextSize = 16
   lvwItems.SingleLineLayout.Label.TextColor= Colors.DarkGray
  
   lvwItems.TwoLinesLayout.ItemHeight = 36dip
   lvwItems.TwoLinesLayout.Label.Top = 0
   lvwItems.TwoLinesLayout.Label.Left = 20dip
   lvwItems.TwoLinesLayout.Label.Width = 290dip
   lvwItems.TwoLinesLayout.Label.Height = 36dip
   lvwItems.TwoLinesLayout.Label.Gravity = Gravity.CENTER_VERTICAL
   lvwItems.TwoLinesLayout.Label.TextColor = Colors.White
   lvwItems.TwoLinesLayout.Label.TextSize = 20
  
   lvwItems.TwoLinesLayout.SecondLabel.Top = 0
   lvwItems.TwoLinesLayout.SecondLabel.Left = 330dip
   lvwItems.TwoLinesLayout.SecondLabel.Width = 170dip
   lvwItems.TwoLinesLayout.SecondLabel.Height = 36dip
   lvwItems.TwoLinesLayout.SecondLabel.Gravity = Gravity.CENTER_VERTICAL
   lvwItems.TwoLinesLayout.SecondLabel.TextColor = Colors.White
   lvwItems.TwoLinesLayout.SecondLabel.TextSize = 20

' ....................

Cheers mj
 

Attachments

  • Capture.JPG
    Capture.JPG
    49.1 KB · Views: 324
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
If these subjects are already contained in a table, and each category is associated to one of them, then you can execute a distinct query to read all subjects active and then loop through your table to get the records associated with each one of them. You can even avoid the two queries by making just one, but then use an if to determine if the current subject is different than the previous and add it as 'header'. In this case you should sort using subject's id.
 
Upvote 0
U

unba1300

Guest
Thanks guys, but I guess I didn't explain very well. It should be easier. I'm not working with changing data. It's just a clickable list I want to present to the user with text that will never change. Listview works fine, but I wanted to add titles or headers as well. Something like this:
Cars:
Honda
Ford
Toyota
People:
German
Policeman
Erel
Foods:
Banana
Apple
Pear
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Here is a sample that displays a ListView as you typed above ..

The Listview_Click will not process if user clicks on a Title/Header .

Cheers mj
 

Attachments

  • ListView Sample.zip
    22.5 KB · Views: 287
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
HEre another example. This one uses the standard-listview and uses different layouts for singleline and twoline-items.

The relevant code from this example is

B4X:
  LV.SingleLineLayout.ItemHeight = 100dip
  LV.SingleLineLayout.Label.TextSize = 20
  LV.SingleLineLayout.Label.TextColor = Colors.Yellow
  LV.SingleLineLayout.Label.Gravity = Gravity.CENTER
   LV.TwoLinesLayout.ItemHeight = 50dip
   LV.TwoLinesLayout.Label.TextColor = Colors.White
   LV.TwoLinesLayout.Label.TextSize = 16
   LV.TwoLinesLayout.SecondLabel.TextColor = Colors.Green
   LV.TwoLinesLayout.SecondLabel.TextSize = 12
 
   For i = 1 To 10
  LV.AddSingleLine("Category #" & i)
     For a = 1 To 20
      LV.AddTwoLines("Item #" & i&"."&a, "This is the second line.")
     'LV.AddTwoLinesAndBitmap("Item #" & i, "This is the second line.", Bitmap1) 
    Next
  Next
 

Attachments

  • listviewwithheader.zip
    7.1 KB · Views: 253
Last edited:
Upvote 0
U

unba1300

Guest
Here is a sample that displays a ListView as you typed above ..

The Listview_Click will not process if user clicks on a Title/Header .

Cheers mj
Thank you very much for taking the time to do that. It should work for my needs.
 
Upvote 0
Top