Search code examples
androiddatabaselistviewswitch-statementandroid-cursoradapter

Switch in Custom Listview


I am populating a ListView via CursorAdapter from SQLite Database Layout contained Switch.

How can I listen to the Switch change event for a particular ListItem?


Solution

  • Register a callback to be invoked when the checked state of this button changes.

    Example:

    mySwitch = (Switch) findViewById(R.id.mySwitch);
    // attach a listener to check for changes in a state
    mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView,
        boolean isChecked) {
        // do something on event
     }
    });
    

    Also check viewholder pattern.