A question about ExpandableListViews in Android

Posted on 2011-05-02
Last Modified: 2012-05-11

       I asked a question relating to this a little while back, but I'm still having a problem with getting it to work...

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;

public class ExpList extends ExpandableListActivity
    static final String colors[] = {

	static final String shades[][] = {
// Shades of grey
		"one","one B",
		"one","two B",
		"one","three B"
// Shades of blue
		"four","four B",
		"five","five B",
		"six","six B"
// Shades of yellow
		"seven","seven B",
		"eight","eight B",
		"nine","	nine B"
// Shades of red
		"ten","ten B",
		"eleven","eleven B",
		"twelve","twelve B"

    /** Called when the activity is first created. */
    public void onCreate(Bundle icicle)
		SimpleExpandableListAdapter expListAdapter =
			new SimpleExpandableListAdapter(
				createGroupList(),	// groupData describes the first-level entries
				R.layout.child_row,	// Layout for the first-level entries
				new String[] { "colorName" },	// Key in the groupData maps to display
				new int[] { },		// Data under "colorName" key goes into this TextView
				createChildList(),	// childData describes second-level entries
				R.layout.child_row,	// Layout for second-level entries
				new String[] { "shadeName", "rgb" },	// Keys in childData maps to display
				new int[] {, }	// Data under the keys above go into these TextViews
		setListAdapter( expListAdapter );
		final ExpandableListView elv = this.getExpandableListView();
				new OnItemSelectedListener()
				 public void onItemSelected(AdapterView<?> parent, View view,int pos, long id) 

				public void onNothingSelected(AdapterView<?> arg0) {
					// TODO Auto-generated method stub
  * Creates the group list out of the colors[] array according to
  * the structure required by SimpleExpandableListAdapter. The resulting
  * List contains Maps. Each Map contains one entry with key "colorName" and
  * value of an entry in the colors[] array.
	private List createGroupList() {
	  ArrayList result = new ArrayList();
	  for( int i = 0 ; i < colors.length ; ++i ) {
		HashMap m = new HashMap();
	    m.put( "colorName",colors[i] );
		result.add( m );
	  return (List)result;

  * Creates the child list out of the shades[] array according to the
  * structure required by SimpleExpandableListAdapter. The resulting List
  * contains one list for each group. Each such second-level group contains
  * Maps. Each such Map contains two keys: "shadeName" is the name of the
  * shade and "rgb" is the RGB value for the shade.
  private List createChildList() {
	ArrayList result = new ArrayList();
	for( int i = 0 ; i < shades.length ; ++i ) {
// Second-level lists
	  ArrayList secList = new ArrayList();
	  for( int n = 0 ; n < shades[i].length ; n += 2 ) {
	    HashMap child = new HashMap();
		child.put( "shadeName", shades[i][n] );
	    child.put( "rgb", shades[i][n+1] );
		secList.add( child );
	  result.add( secList );
	return result;


Open in new window

I still can't figure out how to figure out what child is clicked, and get the data from that particular child.

Could anyone please help me out?

Thanks in advance!
Question by:errang
    LVL 17

    Accepted Solution


    There are two changes you need to make in your code:

    1. Dont use the setOnItemSelectedListener method, use the setOnChildClickListener and setOnGroupClickListener methods instead.

    2. Dont use parent.getItemAtPosition(pos), use parent.getExpandableListAdapter().getGroup(pos)) and parent.getExpandableListAdapter().getChild(groupPos, itemPos)

    Here is a sample code:

    elv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
                public boolean onChildClick(ExpandableListView parent, View view, int groupPos, int itemPos, long l) {
                    System.out.println("Group: " + parent.getExpandableListAdapter().getGroup(groupPos));
                    System.out.println("Item: " + parent.getExpandableListAdapter().getChild(groupPos, itemPos));
                    return false;
    elv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
                public boolean onGroupClick(ExpandableListView parent, View view, int groupPos, long l) {
                    System.out.println("Group: " + parent.getExpandableListAdapter().getGroup(groupPos));
                    return false;

    Open in new window


    Author Closing Comment

    Ah, sweet, thanks!

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Suggested Solutions

    Working settings for French ISP Orange "Prêt à Surfer" SIM cards for data connections only. Can't be found anywhere else !
    Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now