Sajmon

Example of ExpendableList

May 21st, 2012
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. package com.sajmon.ExpendableSample;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import android.app.ExpandableListActivity;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.ExpandableListView;
  10. import android.widget.SimpleExpandableListAdapter;
  11. import android.widget.Toast;
  12.  
  13. public class ExpendableSampleActivity extends ExpandableListActivity {
  14.    
  15.     @Override
  16.     public void onCreate(Bundle savedInstanceState) {
  17.         super.onCreate(savedInstanceState);
  18.         SimpleExpandableListAdapter expandAdapter = new SimpleExpandableListAdapter(this,
  19.                 groupData(), R.layout.group_view, new String[] {"Group Item"}, new int[] {R.id.group_label},
  20.                 childData(), R.layout.child_view, new String[] {"Sub Item"}, new int[] {R.id.child_label});
  21.         setListAdapter(expandAdapter);     
  22.     }
  23.    
  24.    
  25.     private List<HashMap<String, String>> groupData() {
  26.        
  27.         ArrayList<HashMap<String, String>> listData = new ArrayList<HashMap<String, String>>();
  28.         for (int i = 0; i < 10; i++) {
  29.             HashMap<String, String> hashData = new HashMap<String, String>();
  30.             hashData.put("Group Item", "Group Item " + i);
  31.             listData.add( hashData );
  32.         }
  33.         return listData;
  34.     }
  35.    
  36.     private List<ArrayList<HashMap<String, String>>> childData() {
  37.        
  38.         ArrayList<ArrayList<HashMap<String, String>>> listData = new ArrayList<ArrayList<HashMap<String, String>>>();
  39.         for (int i = 0; i < 10; i++) {
  40.             ArrayList<HashMap<String, String>> childData = new ArrayList<HashMap<String, String>>();
  41.             for (int n = 0; n < 3; n++) {
  42.                 HashMap<String, String> hashData = new HashMap<String, String>();
  43.                 hashData.put("Sub Item", "Sub Item " + n);
  44.                 childData.add( hashData );
  45.             }
  46.             listData.add( childData );
  47.         }
  48.         return listData;
  49.     }
  50.    
  51.     @Override
  52.     public void onGroupExpand(int groupPosition) {
  53.         Toast.makeText(ExpendableSampleActivity.this, "Detected expand of Group " + groupPosition, Toast.LENGTH_SHORT).show();
  54.     }
  55.    
  56.     @Override
  57.     public boolean onChildClick(ExpandableListView parent, View view, int groupPosition, int childPosition, long id) {
  58.        
  59.         Toast.makeText(getApplicationContext(),
  60.                 "You clicked on child item " + childPosition + " in Group " + groupPosition, Toast.LENGTH_SHORT).show();
  61.         return super.onChildClick(parent, view, groupPosition, childPosition, id);
  62.     }
  63.    
  64. }
Advertisement
Add Comment
Please, Sign In to add comment