Android Tutorial Custom Expandable ListView

This code is an example of creating an expandable listview using a scrollview.
The layout of each item in the list is created in code and is customizable on a per item basis.
The example is pretty barebones but exhibits purpose.

If people improve on this listview and add more features or make it even more generic, please post back on the forum so the example may evolve.
 

Attachments

  • expandablelv.png
    expandablelv.png
    8.5 KB · Views: 21,018
  • ExpandableListView.zip
    10.3 KB · Views: 4,940

rbsoft

Active Member
Licensed User
Longtime User
Excellent! Very useful. Thank you for it. Just comes in handy.

Rolf
 

maleche

Active Member
Licensed User
Longtime User
Great job. I ran the example and would like to modify it.

I would like make an array of 5 Categories (parents) with varying numbers of sub categories (children) from another array. The sub category is the final user selection i.e.
Cat 1: Cars
Sub Cat: 1 Honda
Sub Cat: 2 GMC
Sub Cat: 3 Chevy
Sub Cat: 4 Ford
Cat 2: Instruments
sub cat: 1 Horns
sub cat: 2 Woodwinds
sub cat: 3 Percussion
sub cat: 4 Strings
sub cat: 5 Electric
Cat 3: Colors
sub Cat: 1 Red
sub Cat: 2 Blue
sub Cat: 3 Green
sub Cat: 4 Yellow
sub Cat: 5 Black
sub Cat: 6 Orange
etc,etc,etc.


Thanks in advance!
 

klaus

Expert
Licensed User
Longtime User
I tried to answer your PM but couldn't because in your account you have selected to not accept private messages.

I'm not sure that the ExpandableListView is the best option. I would use a Spinner for each main item.

I don't say that it's impossible with an ExpandableListView but you would need to modify the definition of the ExpListViewItem Type to adapt it to your specific requirements.

Best regards.
 

mc73

Well-Known Member
Licensed User
Longtime User
I think Klaus is right. I've also used spinners in a similar case and the result was very good.
 

maleche

Active Member
Licensed User
Longtime User
MC73,

Would you be willing to share the code so I can move forward?

Warm regards,
Doyle
 

mc73

Well-Known Member
Licensed User
Longtime User
MC73,

Would you be willing to share the code so I can move forward?

Warm regards,
Doyle

The way I structured it, I don't think it will be helpful. This is what you can do:
Assign an ID to each category, then a SubID to each sub category, which is reflecting to the id of your main categories. When you click on the first spinner, populate spinner 2, with the sub categories, the sub id of which, are equal to the id of the clicked item. And so on...

Example
B4X:
dim items(10,7)
items(0,0)="cars"
items(0,1)="honda"
items(0,2)="gmc"
'and so on
' populate spinner1
for k=0 to 9
if items(k,0).length>0 then spinner1.add(items(k,0))
next
'-------

' then in spinner1_click
'populate spinner2
spinner2.clear
for k=1 to 6
if items(position,k).length>0 then spinner2.add (items(position,k))
next
In my case, I am using an sqlite database, thus I populate spinners by quering for the subid. You can also use a map, almost anything that can store arrays.
 
  • Like
Reactions: GIS

javiers

Active Member
Licensed User
Longtime User
Hello
I am new to b4a.
I would apprecite your hel. I really like your listview.

I have a sqlite table with the following fields:

category_txt sub_category_txt detail_group_txt detail_txt

Gases oxidantes PELIGROS POTENCIALES INCENDIO O EXPLOSION aaaaaaaaaaaaaaa
Gases oxidantes PELIGROS POTENCIALES INCENDIO O EXPLOSION bbbbbbbbbbbbbbb
Gases oxidantes PELIGROS POTENCIALES A LA SALUD ccccccccccccccc
Gases oxidantes SEGURIDAD ROPA PROTECTORA sfgsfgsfgsfdgsdfgsfdg
Gases oxidantes SEGURIDAD EVACUACION rtwretwerweyrrewy

How could adapt your code to make :

Gases oxidantes -> PELIGROS POTENCIALES -> INCENDIO O EXPLOSION -> aaaaaaaaaaaaaaa
bbbbbbbbbbbbbbb
A LA SALUD -> aaaaaaaaaaaaaaa

-> SEGURIDAD -> ROPA PROTECTORA -> sfgsfgsfgsfdgsdfgsfdg
-> EVACUACION -> rtwretwerweyrrewy
 

enrico

Active Member
Licensed User
Longtime User
I was looking at this example, but I have a 5 level hierarchical menu to do and I think I can't use it.
Any advice ?
 

thedesolatesoul

Expert
Licensed User
Longtime User
I was looking at this example, but I have a 5 level hierarchical menu to do and I think I can't use it.
Any advice ?
My advice would be to look at the example and adapt it to a 5-level menu.
Then think about the future design, if it is resource intensive then try out ULV. I have used ULV its good for me, but you have to decide for your own design.
 

GMan

Well-Known Member
Licensed User
Longtime User
Ahoi,

your sample looks pretty nice and i tried to implement it in my app - but fault.

Can you plz give a simple sample of a simple structure like this:
B4X:
1
  1a
      1a1
  1b
      1b1
  1c
     
2
  2a

Can't yet figure out how with all the i's and j's :sign0104:
 
  • Like
Reactions: GIS

thedesolatesoul

Expert
Licensed User
Longtime User
Ahoi,

your sample looks pretty nice and i tried to implement it in my app - but fault.

Can you plz give a simple sample of a simple structure like this:
B4X:
1
  1a
      1a1
  1b
      1b1
  1c
     
2
  2a

Can't yet figure out how with all the i's and j's :sign0104:
Your structure is not simple. (Variable length trees)
In your case we will have to merge GroupType and StructType into a single type, and use a field 'HasChild' to let us know whether or not there are more children. Then we have to recurse from top to bottom. If you are not comfortable with recursion, this is harder (less flexible) to do with fixed loops.
 

GMan

Well-Known Member
Licensed User
Longtime User
Yo...as shown i am a newbie and so i am wondering, for what kind of function you can use this expandable listview, if it only works that way ? :sign0089:
 
Top