fean
fean

Reputation: 546

Adding ExpandableListView in a Layout

I have found this code in internet, an example of ExpandableListView. And it runs perfectly.

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

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

/**
 * Demonstrates expandable lists backed by a Simple Map-based adapter
 */
public class SmplExpandableTest extends ExpandableListActivity {
    private static final String PARENT_KEY = "pKey";
    private static final String CHILD_KEY = "cKey";

    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>>>();

        Map<String, String> curGroupMap = new HashMap<String, String>();
        groupData.add(curGroupMap);
        curGroupMap.put(PARENT_KEY, "Hello");
        curGroupMap.put(CHILD_KEY, "First Order System Response");

        List<Map<String, String>> children = new ArrayList<Map<String, String>>();

        Map<String, String> curChildMap = new HashMap<String, String>();
        children.add(curChildMap);
        curChildMap.put(PARENT_KEY, "World");
        curChildMap.put(CHILD_KEY, "Second Order System");

        childData.add(children);

        // Set up our adapter
        mAdapter = new SimpleExpandableListAdapter(this, groupData,
                android.R.layout.simple_expandable_list_item_1, new String[] {
                        PARENT_KEY, CHILD_KEY }, new int[] {
                        android.R.id.text1, android.R.id.text2 }, childData,
                android.R.layout.simple_expandable_list_item_2, new String[] {
                        PARENT_KEY, CHILD_KEY }, new int[] {
                        android.R.id.text1, android.R.id.text2 });
        setListAdapter(mAdapter);
    }
}

Now i want to insert this ExpandableListView inside a layout, so that I can add a button at the bottom. How can I do this ?

Thanks

Upvotes: 1

Views: 1888

Answers (2)

Tomik
Tomik

Reputation: 23977

To add a Button to the bottom of your screen under ExpandableListView you have to do 2 things:

  1. The activity has to extend from Activity instead of ExpandableListActivity. And you have to set a custom layout.
  2. Create custom layout containing the ExpandableListView and the 'Button`.

The activity would be something like:

public class SmplExpandableTest extends Activity {
    private static final String PARENT_KEY = "pKey";
    private static final String CHILD_KEY = "cKey";

    private ExpandableListAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_layout);
        ExpandableListView elv = (ExpandableListView) findViewById(R.id.elv);

        List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
        List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();

        Map<String, String> curGroupMap = new HashMap<String, String>();
        groupData.add(curGroupMap);
        curGroupMap.put(PARENT_KEY, "Hello");
        curGroupMap.put(CHILD_KEY, "First Order System Response");

        List<Map<String, String>> children = new ArrayList<Map<String, String>>();

        Map<String, String> curChildMap = new HashMap<String, String>();
        children.add(curChildMap);
        curChildMap.put(PARENT_KEY, "World");
        curChildMap.put(CHILD_KEY, "Second Order System");

        childData.add(children);

        // Set up our adapter
        mAdapter = new SimpleExpandableListAdapter(this, groupData,
                android.R.layout.simple_expandable_list_item_1, new String[] {
                        PARENT_KEY, CHILD_KEY }, new int[] {
                        android.R.id.text1, android.R.id.text2 }, childData,
                android.R.layout.simple_expandable_list_item_2, new String[] {
                        PARENT_KEY, CHILD_KEY }, new int[] {
                        android.R.id.text1, android.R.id.text2 });

        elv.setAdapter(mAdapter);
    }
}

Layout res/layout/my_layout.xml would be something like:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
        <ExpandableListView 
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:id="@+id/elv" >
        </ExpandableListView>
        <Button 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btn"
            android:text="@string/btn_text" >
</LinearLayout>

Upvotes: 2

Sanket Kachhela
Sanket Kachhela

Reputation: 10876

you need to use Expandable listview in xml and bind it as BaseExtendedAdapter

here is example

Upvotes: 1

Related Questions