Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 918
  • Last Modified:

A question about ExpandableListViews in Android


       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.app.ExpandableListActivity;
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[] { R.id.childname },		// 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[] { R.id.childname, R.id.rgb }	// 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!
1 Solution
Dejan PažinHead of SW DevelopmentCommented:

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

errangAuthor Commented:
Ah, sweet, thanks!

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now