![]() |
VOOZH | about |
The BaseExpandableListAdapter in Android is an abstract class used to create custom adapters for ExpandableListView, which displays a list of grouped items that can be expanded or collapsed. It provides methods to manage group and child views, define the count of groups and their children, and bind data to the views.
In many android apps, the developer may need to show multi-data for huge main data items. i.e. as per our example, under "Programming languages", we need to show "Python", "Java" etc., and under "Relational database" we need to show "Oracle", "MySQL' etc., For that purpose, we can use "BaseExpandableListAdapter". It is a bridge between the UI component and the data source which fills data in the UI component. It holds the data and then sends the data to the Adapter view then the view can take the data from the Adapter view and shows the data on different views like ExpandableListView. It will provide access to the data of the children (categorized by groups), and also instantiate views for the children and groups. A sample GIF is given below to get an idea about what we are going to do in this article.
Note that we are going to implement this project using both the language Java and Kotlin.
Here is the code snippet for the CustomizedAdapter file in both Java and Kotlin:
CustomizedAdapter File:
Note: Check out the methods getChildView() and getGroupView(). They are used to create the View corresponding to the layout.
| Method | Description |
|---|---|
| getGroupCount() | Returns the total number of groups. |
| getChildrenCount(int groupPosition) | Returns the number of children in a specific group. |
| getGroup(int groupPosition) | Gets the data associated with the group at the specified position. |
| getChild(int groupPosition, int childPosition) | Gets the child data for the given group position. |
| getGroupId(int groupPosition) | Returns the ID for the group. |
| getChildId(int groupPosition, int childPosition) | Returns the child ID for the specified group. |
| getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) | Provides the view for a group item. |
| getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) | Provides the view for a child item. |
| hasStableIds() | Indicates whether IDs are stable across changes. |
| isChildSelectable(int groupPosition, int childPosition) | Indicates whether a child is selectable. |
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Note that select Java/Kotlin as the programming language.
Go to the activity_main.xml file and refer to the following code. Below is the code for the activity_main.xml file.
activity_main.xml:
Go to the app > res > layout > right-click > New > Layout Resource File and name the file as child_items. Below is the code for the child_items.xml file. Here TextView is used for a subset of items Eg: Python.
child_items.xml:
Similarly, create another layout resource file and name the file as group_items. Below is the code for the group_items.xml file. Here TextView is used for the main set of items Eg: Programming_Languages.
group_items.xml:
Go to the app > java > your package name > right-click > New > Java/Kotlin Class and name the file as ChildInfo. Below is the code for the ChildInfo file.
ChildInfo File:
Similarly, create another java class file and name the file as CustomizedAdapter. We have discussed this in the beginning section and also each overridden method. So you may copy the same code and implement it in the project.
Now create another java file and name the file as GroupInformation. Below is the code for the GroupInformation file in both Java and Kotlin.
GroupInformation File:
Go to the MainActivity file and refer to the following code. Below is the code for the MainActivity file in both Java and Kotlin. Comments are added inside the code to understand the code in more detail.
MainActivity File:
On running the app, on the emulator, we can able to view the output as attached in the video. This feature is a much-required feature across many apps.