{"id":1461,"date":"2016-04-08T07:59:41","date_gmt":"2016-04-08T07:59:41","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?p=1461"},"modified":"2019-06-12T13:02:28","modified_gmt":"2019-06-12T13:02:28","slug":"expandablelistadapter-example-android","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/ui\/expandablelistadapter-example-android.html","title":{"rendered":"ExpandableListAdapter Tutorial With Example In Android Studio"},"content":{"rendered":"<p>ExpandableListAdapter is an Adapter that links the ExpandableListView with the underlying data. The implementation of this interface will provide the data for the children and also initiate the views for the children and groups.\u00a0 For customization of List we need to implement ExpandableListAdapter in our custom Adapter.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1455\" src=\"\/ui\/wp-content\/uploads\/2016\/04\/ExpandableListAdapter-in-Android.jpg\" alt=\"ExpandableListAdapter in Android\" width=\"638\" height=\"282\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/04\/ExpandableListAdapter-in-Android.jpg 638w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/04\/ExpandableListAdapter-in-Android-300x133.jpg 300w\" sizes=\"auto, (max-width: 638px) 100vw, 638px\" \/><\/p>\n<p>It is important to remember that\u00a0Adapter acts a\u00a0bridge between UI component and data source that helps us to fill data in UI component. Here in simple words we\u00a0can say an Adapter is that which links an ExpandableListView with the underlying data. The implementation of this Interface will provide access to the data of the children (categorized by groups), and also instantiate views for the children and groups.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1456\" src=\"\/ui\/wp-content\/uploads\/2016\/04\/ExpandableListAdapter-Example.jpg\" alt=\"ExpandableListAdapter Example\" width=\"344\" height=\"419\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/04\/ExpandableListAdapter-Example.jpg 344w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/04\/ExpandableListAdapter-Example-246x300.jpg 246w\" sizes=\"auto, (max-width: 344px) 100vw, 344px\" \/><\/p>\n<p>Below is the example code of ExpandableListAdapter in which we create an custom Adapter class named \u201c MyExpandableListAdapter \u201d and then implements expandable list adapter in that class.<\/p>\n<pre>public class MyExpandableListAdapter implements ExpandableListAdapter {\r\n@Override\r\npublic void registerDataSetObserver(DataSetObserver observer) {\r\n\r\n}\r\n\r\n@Override\r\npublic void unregisterDataSetObserver(DataSetObserver observer) {\r\n\r\n}\r\n\r\n@Override\r\npublic int getGroupCount() {\r\nreturn 0;\r\n}\r\n\r\n@Override\r\npublic int getChildrenCount(int groupPosition) {\r\nreturn 0;\r\n}\r\n\r\n@Override\r\npublic Object getGroup(int groupPosition) {\r\nreturn null;\r\n}\r\n\r\n@Override\r\npublic Object getChild(int groupPosition, int childPosition) {\r\nreturn null;\r\n}\r\n\r\n@Override\r\npublic long getGroupId(int groupPosition) {\r\nreturn 0;\r\n}\r\n\r\n@Override\r\npublic long getChildId(int groupPosition, int childPosition) {\r\nreturn 0;\r\n}\r\n\r\n@Override\r\npublic boolean hasStableIds() {\r\nreturn false;\r\n}\r\n\r\n@Override\r\npublic View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {\r\nreturn null;\r\n}\r\n\r\n@Override\r\npublic View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {\r\nreturn null;\r\n}\r\n\r\n@Override\r\npublic boolean isChildSelectable(int groupPosition, int childPosition) {\r\nreturn false;\r\n}\r\n\r\n@Override\r\npublic boolean areAllItemsEnabled() {\r\nreturn false;\r\n}\r\n\r\n@Override\r\npublic boolean isEmpty() {\r\nreturn false;\r\n}\r\n\r\n@Override\r\npublic void onGroupExpanded(int groupPosition) {\r\n\r\n}\r\n\r\n@Override\r\npublic void onGroupCollapsed(int groupPosition) {\r\n\r\n}\r\n\r\n@Override\r\npublic long getCombinedChildId(long groupId, long childId) {\r\nreturn 0;\r\n}\r\n\r\n@Override\r\npublic long getCombinedGroupId(long groupId) {\r\nreturn 0;\r\n}\r\n}\r\n<\/pre>\n<p>In the above code snippet we see the overrided methods of ExpandableListAdapter which are used to set the data in the ExpandableListView. Below is the description of\u00a0 all these methods:<\/p>\n<p><span style=\"color: #008000;\"><strong>1. registerDataSetObserver(DataSetObserver observer):<\/strong><\/span> This method is used to register an\u00a0observer\u00a0that is called when changes happen to the data used by this\u00a0adapter.<\/p>\n<p><strong>Parameters:<\/strong><\/p>\n<ul>\n<li><em><strong>observer<\/strong><\/em>: The object that gets notified when the data set changes.<\/li>\n<\/ul>\n<p><span style=\"color: #008000;\"><strong>2. unregisterDataSetObserver(DataSetObserver observer):<\/strong><\/span> This method is used to unregister an\u00a0observer\u00a0that is called when changes happen to the data used by this\u00a0adapter.<\/p>\n<p><strong>Parameters:<\/strong><\/p>\n<ul>\n<li><em><strong>observer<\/strong><\/em>: The object that gets notified when the data set changes.<\/li>\n<\/ul>\n<p><span style=\"color: #008000;\"><strong>3. getGroupCount():<\/strong><\/span> This function is used to get the total number of groups.<\/p>\n<p><span style=\"color: #008000;\"><strong>4. getChildrenCount(int groupPosition):<\/strong><\/span> This function gets the number of children in a specified group.<\/p>\n<p><strong>Parameters:<\/strong><\/p>\n<ul>\n<li><strong>groupPosition:<\/strong>\u00a0This\u00a0is the parameter that tells the position for the parent or group of the child and by using that position we calculate the number of children in that group.<\/li>\n<\/ul>\n<p><span style=\"color: #008000;\"><strong>5. getGroup(int groupPosition):<\/strong><\/span> This method gets the data associated with the given group.<\/p>\n<p><strong>Parameters:<\/strong><\/p>\n<ul>\n<li><strong>groupPosition:<\/strong>\u00a0This\u00a0parameter tells the position for the parent or group of the child. This function returns an integer type value<\/li>\n<\/ul>\n<p><span style=\"color: #008000;\"><strong>6. getChild(int groupPosition, int childPosition):<\/strong><\/span> This method gets the data associated with the given child within the given group.<\/p>\n<p><strong>Parameters:<\/strong><\/p>\n<ul>\n<li><strong>groupPosition:<\/strong>\u00a0This is the first parameter that tells the position for the parent or group of the child. This function returns an integer type value<\/li>\n<\/ul>\n<ul>\n<li><strong>childPosition:<\/strong>\u00a0This is the second parameter that tells the position for the child of the given group. This function returns an integer type value.<\/li>\n<\/ul>\n<p><span style=\"color: #008000;\"><strong>7. getGroupId(int groupPosition):<\/strong><\/span> This function is used to get the ID for the group at the given position.<\/p>\n<p><strong>Parameters:<\/strong><\/p>\n<ul>\n<li><strong>groupPosition:<\/strong>\u00a0This parameter tells the position for the parent or group of the child and by using that position we get the ID for the group.<\/li>\n<\/ul>\n<p><span style=\"color: #008000;\"><strong>8. getChildId(int groupPosition, int childPosition):<\/strong><\/span> This function is used to gets the ID for the given child within the given group.<\/p>\n<p><strong>Parameters:<\/strong><\/p>\n<ul>\n<li><strong>groupPosition:<\/strong>\u00a0This is the first parameter that tells the position for the parent or group of the child. This function returns an integer type value<\/li>\n<\/ul>\n<ul>\n<li><strong>childPosition:<\/strong>\u00a0This\u00a0is the second parameter that tells the position for the child of the given group. This function returns an integer type value and by using that value\u00a0we gets the ID for that.<\/li>\n<\/ul>\n<p><span style=\"color: #008000;\"><strong>9. hasStableIds():<\/strong><\/span> This method Indicates that whether the child and group ID\u2019s are stable across changes to the underlying data.<\/p>\n<p><span style=\"color: #008000;\"><strong><em>10.\u00a0<\/em><\/strong><strong>getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent):<\/strong><\/span><em> This method is <\/em>used when we need to create our group or parent View .<\/p>\n<p><strong>Paramters:<\/strong><\/p>\n<ul>\n<li><strong>groupPosition:<\/strong>\u00a0This is the first parameter that tells the position for the parent or group of the child. The function returns an integer type value.<\/li>\n<li><strong>isExpanded:<\/strong>\u00a0This\u00a0is the second parameter that returns Boolean value. It returns true if the current group is expanded and false if it\u2019s not.<\/li>\n<li><strong>convertView:<\/strong>\u00a0This is the fourth parameter that returns View which is used to set the layout for group items.<\/li>\n<li><strong>Parent:<\/strong>\u00a0This\u00a0is the fifth or last parameter that is used to set the view for the parent or group item. \u00a0The eventual parent of this new View<\/li>\n<\/ul>\n<p><span style=\"color: #008000;\"><strong>11. getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent):<\/strong><\/span>\u00a0This method is \u00a0used when we need to create a child View means a child item for a parent or group.<\/p>\n<p><strong>Paramters:<\/strong><\/p>\n<ul>\n<li><strong>groupPosition:<\/strong>\u00a0This is the first parameter that tells the position for the parent(group) of the current child. The function returns an integer type value.<\/li>\n<li><strong>childPosition:<\/strong>\u00a0This is the second parameter that tells the position for current child item of the parent.<\/li>\n<li><strong>isLastChild:<\/strong>\u00a0This is the third parameter that returns Boolean value. It returns true if the current child item is the last child within its group and false if it\u2019s not.<\/li>\n<li><strong>convertView<\/strong>: This\u00a0is the fourth parameter that returns View which is used to set the layout for child items.<\/li>\n<li><strong>Parent:<\/strong> parent is the fifth or last parameter that used to set the view for the parent or group item. \u00a0The eventual parent of this new View.<\/li>\n<\/ul>\n<p><span style=\"color: #008000;\"><strong>12. isChildSelectable(int groupPosition, int childPosition):<\/strong><\/span> This mehod checks whether the child at the specified position is selectable or not. This method returns an Boolean value means true or false. It returns true if the child is selectable and false if it\u2019s not.<\/p>\n<p><strong>Parameters:<\/strong><\/p>\n<ul>\n<li><strong>groupPosition:<\/strong>\u00a0This\u00a0is the first parameter that tells the position for the parent or group of the child. This function returns an integer type value.<\/li>\n<li><strong>childPosition:<\/strong>\u00a0This\u00a0is the second parameter that tells the position for the child of the given group. This function returns an integer type value and by using that value we checks whether the child is selectable or not.<\/li>\n<\/ul>\n<p><span style=\"color: #008000;\"><strong>13. areAllItemsEnabled():<\/strong><\/span> This method tells that all the items of expandable list are enabled or not. This method returns Boolean type value means true or false . It returns true if all the items are enabled.<\/p>\n<p><span style=\"color: #008000;\"><strong>14. isEmpty():<\/strong><\/span> This method returns an Boolean value that tells whether the item is empty or not.<\/p>\n<p><span style=\"color: #008000;\"><strong>15. onGroupExpanded(int groupPosition):<\/strong><\/span> This method is called when a group is collapsed.<\/p>\n<p><strong>Paramters:<\/strong><\/p>\n<ul>\n<li><strong>groupPosition:<\/strong> This\u00a0parameter tells the position for the parent or group of the child that has been expanded<\/li>\n<\/ul>\n<p><span style=\"color: #008000;\"><strong>16. onGroupCollapsed(int groupPosition)<\/strong><\/span>: This method is called when a group is expanded.<\/p>\n<p><strong>Paramters:<\/strong><\/p>\n<ul>\n<li><strong>groupPosition:<\/strong> This\u00a0parameter tells the position for the parent or group of the child that has been collapsed.<\/li>\n<\/ul>\n<p><span style=\"color: #008000;\"><strong>17. getCombinedChildId(long groupId, long childId):<\/strong><\/span> This method is used to get an id for a group that unique any item ( either child or group ) that is in the list.<\/p>\n<p><strong>Parameters:<\/strong><\/p>\n<ul>\n<li><strong>groupId:<\/strong> This parameter tells the ID of the group or parent that contains the child.<\/li>\n<li><strong>childId:<\/strong> This parameter tells the ID of the child.<\/li>\n<\/ul>\n<p><span style=\"color: #008000;\"><strong>18. getCombinedGroupId(long groupId):<\/strong><\/span> This method is used to get an id for a child that unique any item ( either child or group ) that is in the list.<\/p>\n<p><strong>Parameters:<\/strong><\/p>\n<ul>\n<li><strong>groupId<\/strong>: This parameter tells the ID of the group or parent.<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>ExpandableListAdapter\u00a0Example In Android Studio<\/strong><\/h4>\n<p>Below is the example of ExpandableListAdapter in Android where we display an expandable list with song names\u00a0according to category. In this example we display songs list name as group items and their songs names as child items for a particular group. In this we implement setOnChildClickListener and setOnGroupClickListener events and whenever a user clicks on a child or a group item the name of the item ( song or songs list name) is display by using a Toast.<\/p>\n<p>Below is the final output, download code and step by step explanation:<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/ExpandableListAdapterExample\" target=\"_blank\" rel=\"nofollow\">Download Code<\/a><a class=\"help\" title=\"Learn How To Download Code And Import In Android Studio\" href=\"\/androidstudio\/download-code-abhiandroid\" target=\"_blank\" rel=\"nofollow\"> ?<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1458\" src=\"\/ui\/wp-content\/uploads\/2016\/04\/ExpandableListAdapter-Example-In-Android-Studio.gif\" alt=\"ExpandableListAdapter Example In Android Studio\" width=\"300\" height=\"431\" \/><\/p>\n<p><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Create a new project and name it ExpandableListAdapter.<\/p>\n<p><strong><span style=\"color: #008000;\">Step 2:<\/span>\u00a0<\/strong>Open res -&gt; layout -&gt;activity_main.xml (or) main.xml and add following code :<\/p>\n<p>In this step we open an xml file and add the code for displaying ExpandableListView by using its different attributes.<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:orientation=\"vertical\"&gt;\r\n\r\n    &lt;ExpandableListView\r\n        android:id=\"@+id\/simpleExpandableListView\"\r\n        android:layout_width=\"match_parent\"\r\n        android:layout_height=\"fill_parent\"\r\n        android:divider=\"#0f0\"\r\n        android:dividerHeight=\"1dp\" \/&gt;\r\n\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:\u00a0<\/strong><\/span>Create a new xml file for group items Open res -&gt; layout -&gt; group_items.xml and add following code:<\/p>\n<p>In this step we add the code for displaying a Textview for Song Category\u00a0names.<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"55dip\"\r\n    android:orientation=\"vertical\" &gt;\r\n\r\n    &lt;TextView\r\n        android:id=\"@+id\/heading\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:paddingLeft=\"35sp\"\r\n        android:textAppearance=\"?android:attr\/textAppearanceLarge\"\r\n        android:textStyle=\"bold\" \/&gt;\r\n\r\n&lt;\/LinearLayout&gt;<\/pre>\n<p><strong><span style=\"color: #008000;\">Step 4:<\/span>\u00a0<\/strong>Create a new xml file for group items Open res -&gt; layout -&gt; child_items.and add following code:<\/p>\n<p>In this step we add the code for displaying TextView for song\u00a0name.<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;RelativeLayout\r\n    xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:orientation=\"vertical\"&gt;\r\n\r\n    &lt;TextView\r\n        android:id=\"@+id\/childItem\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_alignParentTop=\"true\"\r\n        android:layout_marginLeft=\"10dp\"\r\n        android:textAppearance=\"?android:attr\/textAppearanceMedium\" \/&gt;\r\n\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p><strong><span style=\"color: #008000;\">Step 5:<\/span><\/strong>\u00a0Open src -&gt; package -&gt; MainActivity.java<\/p>\n<p>In this step we open\u00a0MainActivity\u00a0where we add\u00a0the code to\u00a0initiate the ExpandableListView and then add the data to list for displaying in a ExpandableListView using model classes. Finally set the Adapter that fills the data in the ExpandableListView. In this we also implement setOnChildClickListener and setOnGroupClickListener events so\u00a0whenever a user clicks on a child or a group item the name of the item ( song or song list name ) is display by using a Toast.<\/p>\n<pre>package example.abhiandroid.ExpandableListAdapterExample;\r\n\r\nimport android.os.Bundle;\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.view.View;\r\nimport android.widget.ExpandableListView;\r\nimport android.widget.Toast;\r\n\r\nimport java.util.ArrayList;\r\nimport java.util.LinkedHashMap;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n    private LinkedHashMap&lt;String, GroupItemsInfo&gt; songsList = new LinkedHashMap&lt;String, GroupItemsInfo&gt;();\r\n    private ArrayList&lt;GroupItemsInfo&gt; deptList = new ArrayList&lt;GroupItemsInfo&gt;();\r\n\r\n    private MyExpandableListAdapter myExpandableListAdapter;\r\n    private ExpandableListView simpleExpandableListView;\r\n\r\n\r\n    @Override\r\n    public void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n\r\n        \/\/ add data for displaying in expandable list view\r\n        loadData();\r\n\r\n        \/\/get reference of the ExpandableListView\r\n        simpleExpandableListView = (ExpandableListView) findViewById(R.id.simpleExpandableListView);\r\n        \/\/ create the adapter by passing your ArrayList data\r\n        myExpandableListAdapter = new MyExpandableListAdapter(MainActivity.this, deptList);\r\n        \/\/ attach the adapter to the expandable list view\r\n        simpleExpandableListView.setAdapter(myExpandableListAdapter);\r\n\r\n        \/\/ setOnChildClickListener listener for child row click or song name click\r\n        simpleExpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {\r\n            @Override\r\n            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {\r\n                \/\/get the group header\r\n                GroupItemsInfo headerInfo = deptList.get(groupPosition);\r\n                \/\/get the child info\r\n                ChildItemsInfo detailInfo = headerInfo.getSongName().get(childPosition);\r\n                \/\/display it or do something with it\r\n                Toast.makeText(getBaseContext(), \" List And Song Is :: \" + headerInfo.getName()\r\n                        + \"\/\" + detailInfo.getName(), Toast.LENGTH_LONG).show();\r\n                return false;\r\n            }\r\n        });\r\n        \/\/ setOnGroupClickListener listener for group Song List click\r\n        simpleExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {\r\n            @Override\r\n            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {\r\n                \/\/get the group header\r\n                GroupItemsInfo headerInfo = deptList.get(groupPosition);\r\n                \/\/display it or do something with it\r\n                Toast.makeText(getBaseContext(), \" List Name :: \" + headerInfo.getName(),\r\n                        Toast.LENGTH_LONG).show();\r\n\r\n                return false;\r\n            }\r\n        });\r\n\r\n\r\n    }\r\n\r\n    \/\/ load some initial data into out list\r\n    private void loadData() {\r\n\r\n        addProduct(\"Latest Punjabi Songs\", \"Tere Bagair - Amrinder Gill\");\r\n        addProduct(\"Latest Punjabi Songs\", \"Khaab - Akhil\");\r\n        addProduct(\"Latest Punjabi Songs\", \"Dil - Ninja\");\r\n\r\n        addProduct(\"Top 50 Songs\", \"Tere Bagair- Amrinder Gill\");\r\n        addProduct(\"Top 50 Songs\", \"Attt Karti - Jassi Gill\");\r\n\r\n        addProduct(\"Top Of This Week\", \"Khaab- Akhil\");\r\n        addProduct(\"Top Of This Week\", \"Tere Bagair- Amrinder Gill\");\r\n        addProduct(\"Top Of This Week\", \"Gal Sun Ja - Kanwar Chahal\");\r\n\r\n    }\r\n\r\n\r\n    \/\/ here we maintain songsList and songs names\r\n    private int addProduct(String songsListName, String songName) {\r\n\r\n        int groupPosition = 0;\r\n\r\n        \/\/check the hashmap if the group already exists\r\n        GroupItemsInfo headerInfo = songsList.get(songsListName);\r\n        \/\/add the group if doesn't exists\r\n        if (headerInfo == null) {\r\n            headerInfo = new GroupItemsInfo();\r\n            headerInfo.setName(songsListName);\r\n            songsList.put(songsListName, headerInfo);\r\n            deptList.add(headerInfo);\r\n        }\r\n\r\n        \/\/ get the children for the group\r\n        ArrayList&lt;ChildItemsInfo&gt; productList = headerInfo.getSongName();\r\n        \/\/ size of the children list\r\n        int listSize = productList.size();\r\n        \/\/ add to the counter\r\n        listSize++;\r\n\r\n        \/\/ create a new child and add that to the group\r\n        ChildItemsInfo detailInfo = new ChildItemsInfo();\r\n        detailInfo.setName(songName);\r\n        productList.add(detailInfo);\r\n        headerInfo.setPlayerName(productList);\r\n\r\n        \/\/ find the group position inside the list\r\n        groupPosition = deptList.indexOf(headerInfo);\r\n        return groupPosition;\r\n    }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 6:<\/strong><\/span> Create a New Class Open -&gt; package &#8211; &gt; GroupItemsInfo.Java and add the following code.<\/p>\n<p>In this step we create an class for set and get the group item name and child items info according to a particular group. GroupItemsInfo is a model class used to set the name of the group item and child items information from your MainActivity and then get the information within Adapter class. Finally set the value to ExpandableListView.<\/p>\n<pre>package example.abhiandroid.ExpandableListAdapterExample;\r\n\r\nimport java.util.ArrayList;\r\n\r\npublic class GroupItemsInfo {\r\n\r\n    private String listName;\r\n    private ArrayList&lt;ChildItemsInfo&gt; list = new ArrayList&lt;ChildItemsInfo&gt;();\r\n\r\n    public String getName() {\r\n        return listName;\r\n    }\r\n\r\n    public void setName(String songListName) {\r\n        this.listName = songListName;\r\n    }\r\n\r\n    public ArrayList&lt;ChildItemsInfo&gt; getSongName() {\r\n        return list;\r\n    }\r\n\r\n    public void setPlayerName(ArrayList&lt;ChildItemsInfo&gt; songName) {\r\n        this.list = songName;\r\n    }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 7:<\/strong> <\/span>Create a New Class Open -&gt; package &#8211; &gt; ChildItemsInfo.Java and add the following code.<\/p>\n<p>In this step we create a class to\u00a0set and get the name for the child item. ChildItemsInfo\u00a0is also model class used to set the name of the child item from your MainActivity and then get the name within Adapter class. Finally set the value to ExpandableListView.<\/p>\n<pre>package example.abhiandroid.ExpandableListAdapterExample;\r\n\r\npublic class ChildItemsInfo {\r\n\r\n    private String songName = \"\";\r\n\r\n    public String getName() {\r\n        return songName;\r\n    }\r\n\r\n    public void setName(String songName) {\r\n        this.songName = songName;\r\n    }\r\n\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 8:<\/strong><\/span> Create a New Class Open -&gt; package &#8211; &gt; MyExpandableListAdapter.Java and add the following code.<\/p>\n<p>In this step we create a custom adapter named &#8220;MyExpandableListAdapter&#8221; and then implements\u00a0ExpandableListAdapter in that class. Finally we set the data in the ExpandableListView\u00a0from GroupItemsInfo and ChildItemsInfo model classes.<\/p>\n<pre>package example.abhiandroid.ExpandableListAdapterExample;\r\n\r\nimport android.content.Context;\r\nimport android.database.DataSetObserver;\r\nimport android.view.LayoutInflater;\r\nimport android.view.View;\r\nimport android.view.ViewGroup;\r\nimport android.widget.ExpandableListAdapter;\r\nimport android.widget.TextView;\r\n\r\nimport java.util.ArrayList;\r\n\r\n\/**\r\n * Created by Gourav for AbhiAndroid on 19-03-2016.\r\n *\/\r\npublic class MyExpandableListAdapter implements ExpandableListAdapter {\r\n\r\n    private Context context;\r\n    private ArrayList&lt;GroupItemsInfo&gt; teamName;\r\n\r\n    public MyExpandableListAdapter(Context context, ArrayList&lt;GroupItemsInfo&gt; deptList) {\r\n        this.context = context;\r\n        this.teamName = deptList;\r\n    }\r\n    @Override\r\n    public void registerDataSetObserver(DataSetObserver observer) {\r\n\r\n    }\r\n\r\n    @Override\r\n    public void unregisterDataSetObserver(DataSetObserver observer) {\r\n\r\n    }\r\n\r\n    @Override\r\n    public int getGroupCount() {\r\n        return teamName.size();\r\n    }\r\n\r\n    @Override\r\n    public int getChildrenCount(int groupPosition) {\r\n        ArrayList&lt;ChildItemsInfo&gt; productList = teamName.get(groupPosition).getSongName();\r\n        return productList.size();\r\n    }\r\n\r\n    @Override\r\n    public Object getGroup(int groupPosition) {\r\n        return teamName.get(groupPosition);\r\n    }\r\n\r\n    @Override\r\n    public Object getChild(int groupPosition, int childPosition) {\r\n        ArrayList&lt;ChildItemsInfo&gt; productList = teamName.get(groupPosition).getSongName();\r\n        return productList.get(childPosition);\r\n    }\r\n\r\n    @Override\r\n    public long getGroupId(int groupPosition) {\r\n        return groupPosition;\r\n    }\r\n\r\n    @Override\r\n    public long getChildId(int groupPosition, int childPosition) {\r\n        return childPosition;\r\n    }\r\n\r\n    @Override\r\n    public boolean hasStableIds() {\r\n        return true;\r\n    }\r\n\r\n    @Override\r\n    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {\r\n        GroupItemsInfo headerInfo = (GroupItemsInfo) getGroup(groupPosition);\r\n        if (convertView == null) {\r\n            LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);\r\n            convertView = inf.inflate(R.layout.group_items, null);\r\n        }\r\n\r\n        TextView heading = (TextView) convertView.findViewById(R.id.heading);\r\n        heading.setText(headerInfo.getName().trim());\r\n        return convertView;\r\n\r\n    }\r\n\r\n    @Override\r\n    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {\r\n        ChildItemsInfo detailInfo = (ChildItemsInfo) getChild(groupPosition, childPosition);\r\n        if (convertView == null) {\r\n            LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);\r\n            convertView = infalInflater.inflate(R.layout.child_items, null);\r\n        }\r\n        TextView childItem = (TextView) convertView.findViewById(R.id.childItem);\r\n        childItem.setText(detailInfo.getName().trim());\r\n\r\n        return convertView;\r\n    }\r\n\r\n    @Override\r\n    public boolean isChildSelectable(int groupPosition, int childPosition) {\r\n        return true;\r\n    }\r\n\r\n    @Override\r\n    public boolean areAllItemsEnabled() {\r\n        return false;\r\n    }\r\n\r\n    @Override\r\n    public boolean isEmpty() {\r\n        return false;\r\n    }\r\n\r\n    @Override\r\n    public void onGroupExpanded(int groupPosition) {\r\n\r\n    }\r\n\r\n    @Override\r\n    public void onGroupCollapsed(int groupPosition) {\r\n\r\n    }\r\n\r\n    @Override\r\n    public long getCombinedChildId(long groupId, long childId) {\r\n        return 0;\r\n    }\r\n\r\n    @Override\r\n    public long getCombinedGroupId(long groupId) {\r\n        return 0;\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>Now run the App in AVD and you will see ExpandableListView listing song names according to category. This all we created using\u00a0ExpandableListAdapter.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ExpandableListAdapter is an Adapter that links the ExpandableListView with the underlying data. The implementation of this interface will provide the data for the children and also initiate the views for the children and groups.\u00a0 For customization of List we need to implement ExpandableListAdapter in our custom Adapter. It is important to remember that\u00a0Adapter acts a\u00a0bridge &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/expandablelistadapter-example-android.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">ExpandableListAdapter Tutorial With Example In Android Studio<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1455,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[62,1],"tags":[],"class_list":["post-1461","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-adapter","category-archieve"],"acf":[],"psp_head":"<title>ExpandableListAdapter Tutorial With Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Tutorial on ExpandableListAdapter with step by step example In Android Studio. ExpandableListAdapter is an Adapter that links the ExpandableListView with the underlying data.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/expandablelistadapter-example-android.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/1461","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/comments?post=1461"}],"version-history":[{"count":4,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/1461\/revisions"}],"predecessor-version":[{"id":2831,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/1461\/revisions\/2831"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media\/1455"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=1461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/categories?post=1461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/tags?post=1461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}