How should be the data for a SimpleExpandableListAdapter organized

后端 未结 3 1037
走了就别回头了
走了就别回头了 2021-01-18 08:36

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 sim

相关标签:
3条回答
  • 2021-01-18 09:19

    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

    0 讨论(0)
  • 2021-01-18 09:31

    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"}

    0 讨论(0)
  • 2021-01-18 09:35

    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...

    0 讨论(0)
提交回复
热议问题