forum

Home / DeveloperSection / Forums / Add listener to dynamic view in ExpandableListView

Add listener to dynamic view in ExpandableListView

Jayden Bell 3498 17-Oct-2014
I have an expandable list view. I use a custom adapter to load my own .xml layout. Each group has a Switch. I don't know how many groups will be displayed. Now I am wondering how to dynamically add a listener to each switch, in order to react on changes.

ExpandableListView Activity:

    public class ExpandableListView extends Activity {
    private MyCustomAdapter adapter;
    private ExpandableListView listView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail_layout); 
        initViews();        
    }   
    private void initViews() {
        listView = (ExpandableListView) findViewById(R.id.foo);
        adapter = new MyCustomAdapter(this, data); // my custom adapter
        listView.setAdapter(adapter);
        //...   
    }
    //...   
}
MyCustomAdapter

public class MyCustomAdapter extends BaseExpandableListAdapter {
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null); // load layout with switch
        }
        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.myTextView;
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);
        return convertView;
    }
    // ...
}

list_group.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="8dp"
    android:background="#000000">
 
 
    <TextView
        android:id="@+id/myTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:paddingLeft="20dp"
        android:paddingRight="0dp"
        android:paddingEnd="0dp"
        android:paddingStart="20dp"
        android:textSize="17sp"/>
 
 
    <Switch
        android:id="@+id/mySwitch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:focusable="false"
        android:checked="true"
     />
 
 
</RelativeLayout>

How can I add a listener to each "mySwitch"? Is this possible in ExpandableListView?


Updated on 17-Oct-2014

Can you answer this question?


Answer

1 Answers

Liked By