开发者

Expanded List has same children for all parents

Two issues here. Following a tutorial and I need a little help adapting it to my project. 1)The children are the same for each group. For example the Arraylist dining contain the children of the group "Dining Commons" I need to make two more arraylists containing academics buildings and residential buildings and those need to be the children of their respective parents.

2) Is it possible to make the children item clickable? using clicklistener or something like that.

java source.

package com.bogotobogo.android.smplexpandable;

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 SmplExpandable extends ExpandableListActivity {
    private static final String NAME = "NAME";
    private static final String IS_EVEN = "IS_EVEN";

    ArrayList bui开发者_C百科ldings = BuildingList();
    ArrayList diningCommonBuildings = DiningList();

    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 < buildings.size(); i++) {
            Map<String, String> curGroupMap = new HashMap<String, String>();
            groupData.add(curGroupMap);
            curGroupMap.put(NAME, (String) buildings.get(i));
            //curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");

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

            for (int j = 0; j < diningCommonBuildings.size(); j++) {
                Map<String, String> curChildMap = new HashMap<String, String>();
                dining.add(curChildMap);
                curChildMap.put(NAME, (String) diningCommonBuildings.get(j));
                //curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd");
            }
            childData.add(dining);
        }

        // 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);
    }

    private ArrayList BuildingList() {

                ArrayList buildings = new ArrayList();
                buildings.add("Academic Buildings");
                buildings.add("Dining Commons");
                buildings.add("Residential Buildings");
                return buildings;
            }
    private ArrayList DiningList() {

        ArrayList dining = new ArrayList();
        dining.add("Berkshire");
        dining.add("Franklin");
        dining.add("Hampden");
        dining.add("Hampshire");
        dining.add("Worcester");
        return dining;
    }

}


Check out this example,

public class MainExpand extends ExpandableListActivity {

    private ExpandableListAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

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

        for (int i = 0; i < 5; i++) {

            Map<String, String> parentMap = new HashMap<String, String>();
            parentMap.put("lalit", "Parent "+i);
            parent.add(parentMap);

            List<Map<String, String>> child = new ArrayList<Map<String,String>>();
            for (int j = 0; j < 2; j++) {

                Map<String, String> childMap = new HashMap<String, String>();
                childMap.put("lalit", "Child "+j);
                child.add(childMap);
            }
            childGroup.add(child);
        }

        adapter = new SimpleExpandableListAdapter(
                this,
                parent,android.R.layout.simple_expandable_list_item_1,new String[] {"lalit"},new int[] { android.R.id.text1, android.R.id.text2},
                childGroup,android.R.layout.simple_expandable_list_item_2,new String[]{"lalit","even"}, new int[]{android.R.id.text1,android.R.id.text2}
                );
        setListAdapter(adapter);
    }


I used a combination of both of your answers. Thanks. I took advatange of the multidimensional array.

First i created these arrays.

private String[] buildingTypes = {
             "Academic Buildings", "Dining Commons", "Residential Halls" };

private String[][] buildingList = {
             { "Agriculutural Engineering", "Army ROTC", "Arnold House", "Studio Arts Bldg","Bartlett" },
             { "Berkshire", "Franklin", "Hampden", "Hampshire", "Worcester" },
             { "Baker Hall", "Brett Hall", "Brooks Hall", "Brown Hall","Butterfield Hall" },

Then I modified the for loops

for (int i = 0; i < buildingTypes.length; i++) {

            Map<String, String> parentMap = new HashMap<String, String>();
            parentMap.put("lalit", buildingTypes[i]);
            parent.add(parentMap);

            List<Map<String, String>> child = new ArrayList<Map<String,String>>();
            for (int j = 0; j < rows; j++) {

                Map<String, String> childMap = new HashMap<String, String>();
                childMap.put("lalit", buildingList[i][j]);
                child.add(childMap);
            }
            childGroup.add(child);
        }


Here are the answer to both of your questions. If something is not OK for you, just tell me in comments.

Answer of part 1):

Please look here: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html

    private String[][] children = {
            { "Academic1", "Academic2", "Academic3", "Academic4" },
            { "Dining1", "Dining2", "Dining2", "Dining3" },
            { "Residential1", "Residential2" },
    };

Answer of part 2):

http://developer.android.com/reference/android/widget/ExpandableListView.OnChildClickListener.html

onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) Callback method to be invoked when a child in this expandable list has been clicked.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜