Android - ExpandableListView is driving me insane.


  1. Boogs

    Boogs Well-Known Member

    So, this problem has been driving me to the brink of insanity, and I thought I'd start asking around to see if someone else knows the solution. I have an Android app which requires the use of an ExpandableListView, but I want the children of each group to be checkable. Hence I'm using CheckedTextView as you might expect. The solution needs to have the ability to select multiple children as well. Hence my little bit of code:
    Code (Text):
    1.  
    2. listView.setItemsCanFocus(false);
    3. listView.setChoiceMode(ExpandableListView.CHOICE_MODE_MULTIPLE);
    4.  
    Now, I've scoured the Android developer site and everywhere on the internet I could reasonably look for examples on how this is done. The best I've come up with is an example for a ListView with multiple choice items. I have no problems with this example, and it compiles and works fine. However, when I try to make this work on the inherited ExpandableListView, it all falls to pieces and I can't - however hard I try - select multiple (much less a single) CheckBox in the children.

    Does anybody have any idea what I'm doing wrong (code is below)? Is there just no way to have an ExpandableListView "do" multiple choice check boxes? Any help would be appreciated!
    Code (Text):
    1.  
    2. public class Test extends ExpandableListActivity {
    3.    
    4.     final Context mContext = this;
    5.    
    6.     public static final String GROUP_ID = "Group";
    7.     public static final String CHILD_ID = "Child";
    8.     public static final int GROUPS = 2;
    9.     public static final int CHILDREN = 2;
    10.  
    11.     @Override
    12.     protected void onCreate(Bundle savedInstanceState) {
    13.         super.onCreate(savedInstanceState);
    14.        
    15.         /* Set up the adapter for the expandable list */
    16.         setListAdapter(new SimpleExpandableListAdapter(
    17.                 mContext,
    18.                 getGroups(),
    19.                 android.R.layout.simple_expandable_list_item_1,
    20.                 new String[] {GROUP_ID}, new int[] {android.R.id.text1},
    21.                 getChildren(),
    22.                 android.R.layout.simple_list_item_multiple_choice,
    23.                 new String[] {CHILD_ID}, new int[] {android.R.id.text1}));
    24.        
    25.         /* Get the list, and set the choice mode to multiple - why no WORKY? */
    26.         final ExpandableListView listView = getExpandableListView();
    27.         listView.setItemsCanFocus(false);
    28.         listView.setChoiceMode(ExpandableListView.CHOICE_MODE_MULTIPLE);
    29.     }
    30.  
    31.     /* Generate the names of the groups - not the problem, works fine. */
    32.     public List<Map<String, String>> getGroups() {
    33.         List<Map<String, String>> list = new ArrayList<Map<String, String>>();
    34.         Map<String, String> map;
    35.         for (int i = 0; i < GROUPS; ++i) {
    36.             map = new HashMap<String, String>();
    37.             map.put(GROUP_ID, "Group " + i);
    38.             list.add(map); 
    39.         }
    40.         return list;
    41.     }
    42.    
    43.     /* Generates the names of the children - not the problem, works fine. */
    44.     public List<List<Map<String, String>>> getChildren() {
    45.         List<List<Map<String, String>>> list = new ArrayList<List<Map<String, String>>>();
    46.         List<Map<String, String>> subList = new ArrayList<Map<String, String>>();
    47.         Map<String, String> map;
    48.         for (int j = 0; j < CHILDREN; ++j) {
    49.             map = new HashMap<String, String>();
    50.             map.put(CHILD_ID, "Child " + j);
    51.             subList.add(map);
    52.         }
    53.         for (int i = 0; i < GROUPS; ++i) {
    54.             list.add(subList);
    55.         }
    56.         return list;
    57.     }
    58. }
    59.  

    Advertisement
  2. congli.sw

    congli.sw New Member

    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.view.View;
    import android.widget.AdapterView;
    import android.widget.ExpandableListView;
    import android.widget.SimpleExpandableListAdapter;
    import android.widget.Toast;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ExpandableListView.OnChildClickListener;

    public class ExpandableListChoiceActivity extends ExpandableListActivity {
    public static final String GROUP_ID = "Group";
    public static final String CHILD_ID = "Child";
    public static final int GROUPS = 3;
    public static final int CHILDREN = 3;

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

    /* Set up the adapter for the expandable list */
    setListAdapter(new SimpleExpandableListAdapter(
    ExpandableListChoiceActivity.this,
    getGroups(),
    android.R.layout.simple_expandable_list_item_1,
    new String[] {GROUP_ID}, new int[] {android.R.id.text1},
    getChildren(),
    android.R.layout.simple_list_item_multiple_choice,
    new String[] {CHILD_ID}, new int[] {android.R.id.text1}));

    /* Get the list, and set the choice mode to multiple - why no WORKY? */
    final ExpandableListView listView = getExpandableListView();
    listView.setItemsCanFocus(false);
    listView.setChoiceMode(ExpandableListView.CHOICE_MODE_MULTIPLE);

    }
    /* Generate the names of the groups - not the problem, works fine. */
    public List<Map<String, String>> getGroups() {
    List<Map<String, String>> list = new ArrayList<Map<String, String>>();
    Map<String, String> map;
    for (int i = 0; i < GROUPS; ++i) {
    map = new HashMap<String, String>();
    map.put(GROUP_ID, "Group " + i);
    list.add(map);
    }
    return list;
    }

    /* Generates the names of the children - not the problem, works fine. */
    public List<List<Map<String, String>>> getChildren() {
    List<List<Map<String, String>>> list = new ArrayList<List<Map<String, String>>>();
    List<Map<String, String>> subList = new ArrayList<Map<String, String>>();
    Map<String, String> map;
    for (int j = 0; j < CHILDREN; ++j) {
    map = new HashMap<String, String>();
    map.put(CHILD_ID, "Child " + j);
    subList.add(map);
    }
    for (int i = 0; i < GROUPS; ++i) {
    list.add(subList);
    }
    return list;
    }
    }
  3. joeyvanderbie

    joeyvanderbie New Member

  4. nghien_rbc

    nghien_rbc Member

Loading...

Share This Page