Skip to content Skip to sidebar Skip to footer

How Should Be The Data For A Simpleexpandablelistadapter Organized

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

Solution 1:

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;

publicclassSimpleExpandableListExampleActivityextendsExpandableListActivity {
    privatestaticfinalStringNAME="NAME";

    private ExpandableListAdapter mAdapter;

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

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        List<Map<String, String>> groupData = newArrayList<Map<String, String>>();
        List<List<Map<String, String>>> childData = newArrayList<List<Map<String, String>>>();
        for (inti=0; i < group.length; i++) {
            Map<String, String> curGroupMap = newHashMap<String, String>();
            groupData.add(curGroupMap);
            curGroupMap.put(NAME, group[i]);

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

        // Set up our adapter
        mAdapter = newSimpleExpandableListAdapter(this, groupData,
                android.R.layout.simple_expandable_list_item_1,
                newString[] { NAME }, newint[] { android.R.id.text1 },
                childData, android.R.layout.simple_expandable_list_item_2,
                newString[] { NAME }, newint[] { 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...

Solution 2:

You will need two lists or arrays.

Parent (Dev. Team and Data Process team)

The parent list is regular one, you will have to add all the parents i.e. top level items.

Childrean

The main difference between parent and children list, is that children list is a list of lists.

So for example if you need the children of the parent you will do something like this

children.get(parentPosition)

And if you need an specific child

children.get(parentPosition).get(childPosition)

Here you can find an example using BaseExpandableListAdapter

Solution 3:

There are literally so many examples for your requirement!

Anyway, you might want to look into this blog, if you already haven't.

http://www.coderzheaven.com/2011/04/10/expandable-listview-in-android-using-simpleexpandablelistadapter-a-simple-example/

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

OF course, yes! , I mean you can choose to not create layouts, but then again it's your choice and this has very less to do with ExpandableList

The GroupList ideally would contain a list of Teams line List = {"Development Team", "Data Process Team"} (This will predominantly used for display purpose only like calculating the height of the list etc.)

The ChildList ideally would contain a list of lists (Hashmap of Lists rather)

Hashmap> =

Development Team: List = {"John", "Bill"} Data Process Team: List = {"John", "Bill"}

Post a Comment for "How Should Be The Data For A Simpleexpandablelistadapter Organized"