Expandable ListView in ANDROID using SimpleExpandableListAdapter, a simple example.

By | April 10, 2011

Hi all……

Here is a simple example of expandandable ListView in ANDROID.
But I am not going to explain any code, because everything is explained inside the java file.
Make sure to read it.

Expandable ListView

package pack.Coderzheaven;

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

public class ExpandableListDemo extends ExpandableListActivity {

	@SuppressWarnings("unchecked")
	public void onCreate(Bundle savedInstanceState) {
    	try{
    		 super.onCreate(savedInstanceState);
    		 setContentView(R.layout.main);

        SimpleExpandableListAdapter expListAdapter =
			new SimpleExpandableListAdapter(
					this,
					createGroupList(), 				// Creating group List.
					R.layout.group_row,				// Group item layout XML.
					new String[] { "Group Item" },	// the key of group item.
					new int[] { R.id.row_name },	// ID of each group item.-Data under the key goes into this TextView.
					createChildList(),				// childData describes second-level entries.
					R.layout.child_row,				// Layout for sub-level entries(second level).
					new String[] {"Sub Item"},		// Keys in childData maps to display.
					new int[] { R.id.grp_child}		// Data under the keys above go into these TextViews.
				);
			setListAdapter( expListAdapter );		// setting the adapter in the list.

    	}catch(Exception e){
    		System.out.println("Errrr +++ " + e.getMessage());
    	}
    }

	/* Creating the Hashmap for the row */
	@SuppressWarnings("unchecked")
	private List createGroupList() {
	  	  ArrayList result = new ArrayList();
	  	  for( int i = 0 ; i  groupPosition = " + groupPosition);
    	}catch(Exception e){
    		System.out.println(" groupPosition Errrr +++ " + e.getMessage());
    	}
    }
}

The main.xml file.

The child_row.xml

The group_row.xml

The strings.xml (This file contains the string for the color that is used for text in the ListView)

ExpandableList Demo#ffffff#2554C7#347C2C#ff9900#FF00FF#a020f0#778899#C11B17

The manifest.xml

Please leave your comments if the post was useful.
Follow me on facebook, twitter and google plus for more updates.

Download.

Leave a Reply

Your email address will not be published. Required fields are marked *