{"id":1489,"date":"2016-04-12T05:35:30","date_gmt":"2016-04-12T05:35:30","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?p=1489"},"modified":"2019-06-12T12:49:43","modified_gmt":"2019-06-12T12:49:43","slug":"simpleexpandablelistadapter-example-android-studio","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/ui\/simpleexpandablelistadapter-example-android-studio.html","title":{"rendered":"SimpleExpandableListAdapter With Example In Android Studio"},"content":{"rendered":"<p>SimpleExpandableListAdapter is an Adapter which is used to map the static data to group and child views defined in an XML (layout) file. We can separately specify the data backing to the group as a List of Map. Each entry in a ArrayList corresponds to one group in the ExpandableListView. The map contains the data for each row.<\/p>\n<p>We can also specify an XML file that defines the views used to display a group, and a mapping from keys in the Map to specific views. This process is similar for a child, except it is one level deeper so the data backing is specified as a List&lt;list&gt;, where the first List correspond to the group of the child and the second List correspond to the position of the child within that group, and finally the Map holds the data for the particular child.<\/p>\n<pre>public\u00a0SimpleExpandableListAdapter\u00a0(Context\u00a0context,\u00a0List&lt;?\u00a0extends\u00a0Map&lt;String,\u00a0?&gt;&gt; groupData, int groupLayout,\u00a0String[]groupFrom, int[] groupTo,\u00a0List&lt;?\u00a0extends\u00a0List&lt;?\u00a0extends\u00a0Map&lt;String,\u00a0?&gt;&gt;&gt; childData, int childLayout,\u00a0String[]\u00a0childFrom, int[] childTo)\r\n<\/pre>\n<p>In Above code snippet we show the implementation of SimpleExpandableListAdapter in Android. Below is the description of all the parameters used for implementation of a SimpleExpandableListAdapter to display ExpandableListView.<\/p>\n<p><strong>Parameters Used In SimpleExpandableListAdapter:<\/strong><\/p>\n<p><span style=\"color: #008000;\"><strong>1. context:<\/strong><\/span>\u00a0This\u00a0parameter is used to pass the context means the reference of current class. this is a keyword used to show the current class reference. We can also used getApplicationContext(), getActivity() in the place of this keyword. getApplicationContext() is used in a Activity and getActivity() is used in a Fragment.<\/p>\n<p><span style=\"color: #008000;\"><strong>2. groupData:<\/strong><\/span>\u00a0This is a\u00a0List of Maps. Each entry in a List corresponds to one group. The Maps contain\u2019s\u00a0 the data of each group and should include all the entries specified in &#8220;groupFrom&#8221; string array.<\/p>\n<p><strong><span style=\"color: #008000;\">3. groupLayout:<\/span>\u00a0<\/strong>This parameter is\u00a0used to set the layout(xml file) for the group items in which you have a TextView, ImageView\u00a0or any other view.<\/p>\n<p><span style=\"color: #008000;\"><strong>4. groupFrom:<\/strong><\/span> This \u00a0is a\u00a0string array or called a list of column names that will be added to a Map associated with the group.<\/p>\n<p><span style=\"color: #008000;\"><strong>5. groupTo:<\/strong><\/span> The parameter\u00a0is an integer array used to store the Id\u2019s of the views. The views that should display column in the &#8220;groupFrom&#8221; parameter. The first N views in this list are given the values of the first N columns in the \u201cgroupFrom\u201d parameter.<\/p>\n<p><span style=\"color: #008000;\"><strong>6. childData:<\/strong><\/span>\u00a0This is a\u00a0List of Maps. Each entry in a List corresponds to the child of a group. The Maps contain\u2019s\u00a0 the data for child items and should include all the entries specified in &#8220;childFrom&#8221; string array.<\/p>\n<p><span style=\"color: #008000;\"><strong>7. childLayout:<\/strong><\/span>\u00a0This\u00a0parameter is used to set the layout(XML file) for the child items in which you have a TextView, ImageView or any other view.<\/p>\n<p><span style=\"color: #008000;\"><strong>8. childFrom:<\/strong><\/span>\u00a0This is a\u00a0string array or called a list of column names that will be added to a Map associated with the child of a group.<\/p>\n<p><span style=\"color: #008000;\"><strong>9. childTo:<\/strong>\u00a0<\/span>This\u00a0is an\u00a0integer array used to store the Id\u2019s of the views. The views that should display column in the &#8220;childFrom&#8221; parameter. The first N views in this list are given the values of the first N columns in the \u201cchildFrom\u201d parameter.<\/p>\n<hr \/>\n<h4><strong>SimpleExpandableListAdapter Example In Android Studio<\/strong><\/h4>\n<p>Below is the example of SimpleExpandableListAdapter in Android\u00a0where\u00a0we display an ExpandableListView with Animal and Bird names. In this example we display two group items one is animal and other is birds.\u00a0Whenever a user click on a group the list expands and displays the child items. It means when a user click on \u201cBirds\u201d the list expands with bird names. 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 is displayed by using a Toast.<\/p>\n<p>Below you can download complete project code, final output and step by step explanation:<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/SimpleExpandableListAdapterExample\" target=\"_blank\">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-1492\" src=\"\/ui\/wp-content\/uploads\/2016\/04\/SimpleExpandableListAdapter-Example-in-Android-Studio.jpg\" alt=\"SimpleExpandableListAdapter Example in Android Studio\" width=\"260\" height=\"398\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/04\/SimpleExpandableListAdapter-Example-in-Android-Studio.jpg 260w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/04\/SimpleExpandableListAdapter-Example-in-Android-Studio-196x300.jpg 196w\" sizes=\"auto, (max-width: 260px) 100vw, 260px\" \/><\/p>\n<p><strong><span style=\"color: #008000;\">Step 1<\/span>:<\/strong>\u00a0Create a new project and name it SimpleExpandableListAdapter.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span>\u00a0Open res -&gt; layout -&gt; activity_main.xml (or) main.xml and add following code.<\/p>\n<p>In this step we open an XML\u00a0file and add the code for displaying a 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=\"#f00\"\r\n        android:dividerHeight=\"1dp\" \/&gt;\r\n\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p><strong><span style=\"color: #008000;\">Step 3<\/span>:<\/strong>\u00a0Create a new XML\u00a0file 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 group items.<\/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:gravity=\"center\"\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>:<\/strong>\u00a0Create a new xml file for group items Open res -&gt; layout -&gt; child_items.xml and add following code:<\/p>\n<p>In this step we add the code for displaying TextView for child items means for animal and birds names.<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;RelativeLayout android:layout_width=\"match_parent\" android:layout_height=\"match_parent\"\r\n    android:orientation=\"vertical\" xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"&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=\"5dp\"\r\n        android:textAppearance=\"?android:attr\/textAppearanceMedium\" \/&gt;\r\n\r\n&lt;\/RelativeLayout&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 5:<\/strong><\/span>\u00a0Open src -&gt; package -&gt; MainActivity.Java<\/p>\n<p>In this step we open\u00a0MainActivity\u00a0and\u00a0add the code to\u00a0initiate the ExpandableListView\u00a0and add the data to lists for displaying in an ExpandableListView. Finally set the Adapter to fill the group and child data in the list. In this we implement setOnChildClickListener() and setOnGroupClickListener() events. Whenever a user clicks on a child or a group item, the name of the item is display by using a Toast.<\/p>\n<pre>package example.abhiandroid.SimpleExpandableListAdapterexample;\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.SimpleExpandableListAdapter;\r\nimport android.widget.Toast;\r\n\r\nimport java.util.ArrayList;\r\nimport java.util.HashMap;\r\nimport java.util.List;\r\nimport java.util.Map;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n    private static final String NAME = \"NAME\";\r\n\r\n    private SimpleExpandableListAdapter mAdapter;\r\n    ExpandableListView simpleExpandableListView;\r\n    \/\/ string arrays for group and child items\r\n    private String groupItems[] = {\"Animals\", \"Birds\"};\r\n    private String[][] childItems = {{\"Dog\", \"Cat\", \"Tiger\"}, {\"Crow\", \"Sparrow\"}};\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        \/\/  initiate the expandable list view\r\n        simpleExpandableListView = (ExpandableListView) findViewById(R.id.simpleExpandableListView);\r\n        \/\/ create lists for group and child items\r\n        List&lt;Map&lt;String, String&gt;&gt; groupData = new ArrayList&lt;Map&lt;String, String&gt;&gt;();\r\n        List&lt;List&lt;Map&lt;String, String&gt;&gt;&gt; childData = new ArrayList&lt;List&lt;Map&lt;String, String&gt;&gt;&gt;();\r\n        \/\/ add data in group and child list\r\n        for (int i = 0; i &lt; groupItems.length; i++) {\r\n            Map&lt;String, String&gt; curGroupMap = new HashMap&lt;String, String&gt;();\r\n            groupData.add(curGroupMap);\r\n            curGroupMap.put(NAME, groupItems[i]);\r\n\r\n            List&lt;Map&lt;String, String&gt;&gt; children = new ArrayList&lt;Map&lt;String, String&gt;&gt;();\r\n            for (int j = 0; j &lt; childItems[i].length; j++) {\r\n                Map&lt;String, String&gt; curChildMap = new HashMap&lt;String, String&gt;();\r\n                children.add(curChildMap);\r\n                curChildMap.put(NAME, childItems[i][j]);\r\n            }\r\n            childData.add(children);\r\n        }\r\n        \/\/ define arrays for displaying data in Expandable list view\r\n        String groupFrom[] = {NAME};\r\n        int groupTo[] = {R.id.heading};\r\n        String childFrom[] = {NAME};\r\n        int childTo[] = {R.id.childItem};\r\n\r\n\r\n        \/\/ Set up the adapter\r\n        mAdapter = new SimpleExpandableListAdapter(this, groupData,\r\n                R.layout.group_items,\r\n                groupFrom, groupTo,\r\n                childData, R.layout.child_items,\r\n                childFrom, childTo);\r\n        simpleExpandableListView.setAdapter(mAdapter);\r\n\r\n        \/\/ perform set on group click listener event\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\r\n                \/\/ display a toast with group name whenever a user clicks on a group item\r\n                Toast.makeText(getApplicationContext(), \"Group Name Is :\" + groupItems[groupPosition], Toast.LENGTH_LONG).show();\r\n\r\n                return false;\r\n            }\r\n        });\r\n        \/\/ perform set on child click listener event\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\r\n                \/\/ display a toast with child name whenever a user clicks on a child item\r\n                Toast.makeText(getApplicationContext(), \"Child Name Is :\" + childItems[groupPosition][childPosition], Toast.LENGTH_LONG).show();\r\n                return false;\r\n            }\r\n        });\r\n    }\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<p>Now run the App and you will Animal and Birds names listed in ExpandableListView. This we have done using SimpleExpandableListAdapter.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SimpleExpandableListAdapter is an Adapter which is used to map the static data to group and child views defined in an XML (layout) file. We can separately specify the data backing to the group as a List of Map. Each entry in a ArrayList corresponds to one group in the ExpandableListView. The map contains the data &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/simpleexpandablelistadapter-example-android-studio.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">SimpleExpandableListAdapter With Example In Android Studio<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1492,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[62,1],"tags":[],"class_list":["post-1489","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-adapter","category-archieve"],"acf":[],"psp_head":"<title>SimpleExpandableListAdapter With Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Tutorial on SimpleExpandableListAdapter with step by step explanation of example In Android Studio. SimpleExpandableListAdapter is an Adapter which is used to map the static data to group and child views defined in an XML (layout) file. We can separately specify the data backing to the group as a List of Map.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/simpleexpandablelistadapter-example-android-studio.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/1489","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=1489"}],"version-history":[{"count":3,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/1489\/revisions"}],"predecessor-version":[{"id":2826,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/1489\/revisions\/2826"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media\/1492"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=1489"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/categories?post=1489"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/tags?post=1489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}