Mukunda
Mukunda

Reputation: 1623

Adding listener to child nodes in Expandable List Item

I have used a code given in tutorial http://mylifewithandroid.blogspot.com/2010/12/expandable-list-and-checkboxes.html I have attached an image of the ExpandableListView enter image description here

SimpleExpandableListAdapter expListAdapter =
        new SimpleExpandableListAdapter(
            this,
            createGroupList(),  
            R.layout.child_row, 
            new String[] { "CityName" },
            new int[] { R.id.childname },       
            createChildList(),  
            R.layout.child_row, 
            new String[] { "citydetails", "city" }, 
            new int[] { R.id.childname, R.id.city}  
        );

    setListAdapter( expListAdapter );

I need some code example on how to add listeners to individual child nodes for example clicking on Address should take the user to another activity. Looking forward to your reply. thanks.

Upvotes: 2

Views: 3159

Answers (2)

Scott
Scott

Reputation: 1313

You can also have your activity override the the onChildClick method:

@Override
public boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long id) {
    //implement logic to start the appropriate activity for this child.
}

From the android dev site: onChildClick.

I also recommend downloading the API Demos. You can learn more on setting it up here.

Upvotes: 1

Code Poet
Code Poet

Reputation: 11437

AFAIK, you cannot add listener's to your child views using the example above. You will have to have a custom adapter. In this custom adapter's getChildView and getGroupView you can provide your own listeners. Eg.

public View getGroupView(
            int groupPosition, 
            boolean isExpanded,
            View convertView, 
            ViewGroup parent) {

        ViewGroup viewGroup = null;
        if (convertView == null) {
            LayoutInflater li = (LayoutInflater) getApplication().getSystemService(LAYOUT_INFLATER_SERVICE);
            if (li != null) {
                viewGroup = (ViewGroup) li.inflate(R.layout.listitems_group, parent, false);
            }
        } else {
            viewGroup = (ViewGroup) convertView;
        }

            Button button = (Button) viewGroup.findViewById(R.id.group_button);
            button.setOnClickListener(new OnClickListener() {
                 void onClick(View v) {
                     // Do what you want here
                 }
            });
            return viewGroup;
}

Here I have buttons in my GroupView which I want to add onClick listener's for.

Upvotes: 1

Related Questions