• Introducing XDA Computing: Discussion zones for Hardware, Software, and more!    Check it out!
  • Fill out your device list and let everyone know which phones you have!    Edit Your Device Inventory

[Q] No idea how to load onChildClick in my ExpandableListView.

Search This thread

nex7er

Senior Member
Feb 3, 2008
449
163
Bergen
I have an expandable list view with 2 parents and 3 children. I want to open a dialog based on each click. I can't find any examples showing you how to call something based on positions. At least not with the ExpandableListView tutorial I followed.


Code:
public class MainActivity extends Activity implements OnClickListener {

	private LinkedHashMap<String, HeaderInfo> myDepartments = new LinkedHashMap<String, HeaderInfo>();
	private ArrayList<HeaderInfo> deptList = new ArrayList<HeaderInfo>();

	private MyListAdapter listAdapter;
	private ExpandableListView myList;

   [user=439709]@override[/user]
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// Just add some data to start with
		loadData();

		// get reference to the ExpandableListView
		myList = (ExpandableListView) findViewById(R.id.myList);
		// create the adapter by passing your ArrayList data
		listAdapter = new MyListAdapter(MainActivity.this, deptList);
		// attach the adapter to the list
		myList.setAdapter(listAdapter);
		// listener for child row click
		myList.setOnChildClickListener(myListItemClicked);
		// listener for group heading click
		myList.setOnGroupClickListener(myListGroupClicked);
	}

	// load some initial data into out list
	private void loadData() {

		addProduct("Parent One", "Child One");
		addProduct("Parent One", "Child Two");
		addProduct("Parent One", "Child Three");

		addProduct("Parent Two", "Child One");
		addProduct("Parent Two", "Child Two");
		addProduct("Parent Two", "Child Three");

	}

	// our child listener
	private OnChildClickListener myListItemClicked = new OnChildClickListener() {

	   [user=439709]@override[/user]
		public boolean onChildClick(ExpandableListView parent, View v,
				int groupPosition, int childPosition, long id) {
			// Create a switch that switches on the specific child position.

			// get the group header
			HeaderInfo headerInfo = deptList.get(groupPosition);
			// get the child info
			DetailInfo detailInfo = headerInfo.getProductList().get(
					childPosition);
			// display it or do something with it

			// custom dialog
			final Dialog dialog = new Dialog(MainActivity.this);
			dialog.setContentView(R.layout.cdialog);
			// dialog.setTitle(R.id.titlebar);
			dialog.setTitle(R.string.titlebar);
			dialog.show();

			return false;
		}
	};

	// our group listener

	private OnGroupClickListener myListGroupClicked = new OnGroupClickListener() {

		public boolean onGroupClick(ExpandableListView parent, View v,
				int groupPosition, long id) {

			// get the group header HeaderInfo headerInfo =
			deptList.get(groupPosition);
			// display it or do something with it

			return false;
		}

	};


I can get a custom dialog open if I click a child, but it's not set to any specific parent and child.

Any ideas?



EDIT ADD: Got it. Tried a switch/case like this and it worked. Finally! After two days of trying to understand it.:fingers-crossed:

Code:
switch(groupPosition) {
        case 1: 
            switch (childPosition) {
            case 0: 
                Intent protheanIntent = new Intent(Codex.this, CodexProthean.class);
                Codex.this.startActivity(protheanIntent);
                break;              
            case 1:
                Intent rachniIntent = new Intent(Codex.this, CodexRachni.class);
                Codex.this.startActivity(rachniIntent);
                break;
            }
        case 2:
            switch (childPosition) {
            case 2: 
                Intent asariIntent = new Intent(Codex.this, CodexAsari.class);
                Codex.this.startActivity(asariIntent);
                break;
            }
        }
 
Last edited: