Simple Expandable List Example

3

Description:
This example shows how to create Expandable list using simple default adapter.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it SimpleExpandableListExample.

2.) Run for output.

Steps:

1.) Create a project named SimpleExpandableListExample and set the information as stated in the image.

Build Target: Android 2.3.3
Application Name: SimpleExpandableListExample
Package Name: com.example
Activity Name: SimpleExpandableListExampleActivity
Min SDK Version: 10

2.) Open SimpleExpandableListExampleActivity.java file and write following code there:

package com.example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.widget.ExpandableListAdapter;
import android.widget.SimpleExpandableListAdapter;

public class SimpleExpandableListExampleActivity extends ExpandableListActivity {
    private static final String NAME = "NAME";
    private static final String IS_EVEN = "IS_EVEN";
    
    private ExpandableListAdapter mAdapter;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
        List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
        for (int i = 0; i < 20; i++) {
            Map<String, String> curGroupMap = new HashMap<String, String>();
            groupData.add(curGroupMap);
            curGroupMap.put(NAME, "Item " + i);
            curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");
            
            List<Map<String, String>> children = new ArrayList<Map<String, String>>();
            for (int j = 0; j < 5; j++) {
                Map<String, String> curChildMap = new HashMap<String, String>();
                children.add(curChildMap);
               // curChildMap.put(NAME, "Child " + j);
                curChildMap.put(IS_EVEN, (j % 2 == 0) ? "Hello " + j: "Good Morning "+ j);
            }
            childData.add(children);
        }
        
        // Set up our adapter
        mAdapter = new SimpleExpandableListAdapter(
                this,
                groupData,
                android.R.layout.simple_expandable_list_item_1,
                new String[] { NAME, IS_EVEN },
                new int[] { android.R.id.text1, android.R.id.text2 },
                childData,
                android.R.layout.simple_expandable_list_item_2,
                new String[] { NAME, IS_EVEN },
                new int[] { android.R.id.text1, android.R.id.text2 }
                );
        setListAdapter(mAdapter);
    }

}

3.) Compile and build the project.

3 Responses

  1. ariel says:

    Muchas gracias… de gran utilidad :)

  2. Michael says:

    I’ve read hundreds (if not thousands) of tutorials and examples while developing Android applications, but I never wrote any comment if anything helped me – yes I will change that about myself.

    After realizing that this saved my damned day I just wanted to say: Thank you!

  3. Sagar says:

    Is it possible to open expandableList on a button click in a activity ? I did succeed but list is hidden and not shown completely. For example like drop down list.

Leave a Reply


5 × = forty five

Proudly designed by Theme Junkie.