Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should be the data for a SimpleExpandableListAdapter organized

Tags:

android

I am trying to create a ExpandableListView with the SimpleExpandableListAdapter(I know I can extend a new adapter myself, but I want to try the simpleExapndableListAdapter).

Now I meet problems when I try to create a new instance of SimpleExpandableListAdapter.

Event I read the references, I am not exactly sure what does the parameters mean especially the groupData and the childData.

Though I know I have to create a list of Map and a list of list of Map, but what data should be put there? How to organized them?

For example, this is the data I want to display, how to organize them?

++Development Team
  John
  Bill
++Data Process Team
  Alice
  David

BTW, does it mean that I have to create two layout for the group and the child view?

I have googled and read the tutorial again and again, but I can not understand it.

I hope someone can give ma an explain.

like image 775
hguser Avatar asked Aug 21 '13 07:08

hguser


1 Answers

Here is simple sample you wanted, it will clear all your doubts..

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 ExpandableListAdapter mAdapter;

    private String group[] = {"Development" , "Data Process Team"};
    private String[][] child = { { "John", "Bill" }, { "Alice", "David" } };

    @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 < group.length; i++) {
            Map<String, String> curGroupMap = new HashMap<String, String>();
            groupData.add(curGroupMap);
            curGroupMap.put(NAME, group[i]);

            List<Map<String, String>> children = new ArrayList<Map<String, String>>();
            for (int j = 0; j < child[i].length; j++) {
                Map<String, String> curChildMap = new HashMap<String, String>();
                children.add(curChildMap);
                curChildMap.put(NAME, child[i][j]);
            }
            childData.add(children);
        }

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

}

All you have to do is

  • Create an Android project with SimpleExpandableListExampleActivity as your main activity..
  • Copy-paste the code given in that activity.
  • Thats it.. Run your code...

Hope this helps...

like image 56
CRUSADER Avatar answered Nov 15 '22 09:11

CRUSADER