Expandable List Help Needed!


Last Updated:

  1. destroythenseek

    destroythenseek New Member This Topic's Starter

    Joined:
    Mar 30, 2009
    Messages:
    1
    Likes Received:
    0
    Hey,
    I have an expandable list that looks something like..


    Code (Text):
    1. public class ExpandableList3 extends ExpandableListActivity {
    2.     private static final String NAME = "NAME";
    3.     private static final String IS_EVEN = "IS_EVEN";
    4.    
    5.     private ExpandableListAdapter mAdapter;
    6.    
    7.     @Override
    8.     public void onCreate(Bundle savedInstanceState) {
    9.         super.onCreate(savedInstanceState);
    10.  
    11.         List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
    12.         List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
    13.         for (int i = 0; i < 20; i++) {
    14.             Map<String, String> curGroupMap = new HashMap<String, String>();
    15.             groupData.add(curGroupMap);
    16.             curGroupMap.put(NAME, "Group " + i);
    17.             curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");
    18.            
    19.             List<Map<String, String>> children = new ArrayList<Map<String, String>>();
    20.             for (int j = 0; j < 15; j++) {
    21.                 Map<String, String> curChildMap = new HashMap<String, String>();
    22.                 children.add(curChildMap);
    23.                 curChildMap.put(NAME, "Child " + j);
    24.                 curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd");
    25.             }
    26.             childData.add(children);
    27.         }
    28.        
    29.         // Set up our adapter
    30.         mAdapter = new SimpleExpandableListAdapter(
    31.                 this,
    32.                 groupData,
    33.                 android.R.layout.simple_expandable_list_item_1,
    34.                 new String[] { NAME, IS_EVEN },
    35.                 new int[] { android.R.id.text1, android.R.id.text2 },
    36.                 childData,
    37.                 android.R.layout.simple_expandable_list_item_2,
    38.                 new String[] { NAME, IS_EVEN },
    39.                 new int[] { android.R.id.text1, android.R.id.text2 }
    40.                 );
    41.         setListAdapter(mAdapter);
    42.     }
    I want to make it so that when I click on one... It'll open a designated new window.
    Help would be really appreciated.

    Thanks,
    Anton
     

    Advertisement
  2. fixedd

    fixedd New Member

    Joined:
    May 2, 2009
    Messages:
    3
    Likes Received:
    0
    I'm not on my dev machine so I can't test this, but I think something like this might be what you're looking for.

    1st: make the class implement OnClickListener
    Code (Text):
    1. public class ExpandableList3 extends ExpandableListActivity implements OnClickListener {
    2nd: Add the code to handle the click:
    Code (Text):
    1.  
    2.     @Override
    3.     public void onClick(View v) {
    4.         switch (v.getId()) {
    5.             case R.id.item_X:
    6.                 // do something
    7.             case R.id.item_Y:
    8.                 // do something else
    9.         }
    10.     }
    11.  
    12.  
     
  3. ChiefPMM

    ChiefPMM New Member

    Joined:
    Aug 23, 2010
    Messages:
    2
    Likes Received:
    0
    I would be very interested in a solution to this expandable list as I am new to Android. The suggested solution doesn't appear to solve the issue unless I'm doing something incorrectly. :)
     
  4. ChiefPMM

    ChiefPMM New Member

    Joined:
    Aug 23, 2010
    Messages:
    2
    Likes Received:
    0
    Were you able to find a suitable solution?
     
  5. aattaa

    aattaa Member

    Joined:
    Feb 12, 2011
    Messages:
    9
    Likes Received:
    0
    Hi,
    I need help!!! my code is near the page. And my question is:

    I want to show the word image by clicking on the word. How can I create onclicklistener in the ExpandableList?

    Thanks

    public class Categories extends ExpandableListActivity {

    private static final String NAME = "NAME";


    private ExpandableListAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    DBManager db = new DBManager(getApplicationContext());

    List<String> header = db.selectAllCategories();
    List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
    List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();

    for (String category : header) {
    Map<String, String> curGroupMap = new HashMap<String, String>();
    groupData.add(curGroupMap);
    curGroupMap.put(NAME, category);


    List<String> categoryWords = db.selectWordsFromCategory(category);

    List<Map<String, String>> children = new ArrayList<Map<String, String>>();
    for (String word : categoryWords) {
    Map<String, String> curChildMap = new HashMap<String, String>();
    children.add(curChildMap);

    curChildMap.put(NAME, word);

    }
    childData.add(children);
    }


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


    }
     

Share This Page

Loading...