Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sajmon.ExpendableSample;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import android.app.ExpandableListActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ExpandableListView;
- import android.widget.SimpleExpandableListAdapter;
- import android.widget.Toast;
- public class ExpendableSampleActivity extends ExpandableListActivity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- SimpleExpandableListAdapter expandAdapter = new SimpleExpandableListAdapter(this,
- groupData(), R.layout.group_view, new String[] {"Group Item"}, new int[] {R.id.group_label},
- childData(), R.layout.child_view, new String[] {"Sub Item"}, new int[] {R.id.child_label});
- setListAdapter(expandAdapter);
- }
- private List<HashMap<String, String>> groupData() {
- ArrayList<HashMap<String, String>> listData = new ArrayList<HashMap<String, String>>();
- for (int i = 0; i < 10; i++) {
- HashMap<String, String> hashData = new HashMap<String, String>();
- hashData.put("Group Item", "Group Item " + i);
- listData.add( hashData );
- }
- return listData;
- }
- private List<ArrayList<HashMap<String, String>>> childData() {
- ArrayList<ArrayList<HashMap<String, String>>> listData = new ArrayList<ArrayList<HashMap<String, String>>>();
- for (int i = 0; i < 10; i++) {
- ArrayList<HashMap<String, String>> childData = new ArrayList<HashMap<String, String>>();
- for (int n = 0; n < 3; n++) {
- HashMap<String, String> hashData = new HashMap<String, String>();
- hashData.put("Sub Item", "Sub Item " + n);
- childData.add( hashData );
- }
- listData.add( childData );
- }
- return listData;
- }
- @Override
- public void onGroupExpand(int groupPosition) {
- Toast.makeText(ExpendableSampleActivity.this, "Detected expand of Group " + groupPosition, Toast.LENGTH_SHORT).show();
- }
- @Override
- public boolean onChildClick(ExpandableListView parent, View view, int groupPosition, int childPosition, long id) {
- Toast.makeText(getApplicationContext(),
- "You clicked on child item " + childPosition + " in Group " + groupPosition, Toast.LENGTH_SHORT).show();
- return super.onChildClick(parent, view, groupPosition, childPosition, id);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment