I am creating CheckBoxes Dynamically in an Android Class (Not Activity). So I need to add onClick Action Listener to my checkboxes, how to implement this.
I am using following code.
public class DataBaseAdapter extends SQLiteOpenHelper
{
...//onCreate and onUpdate
...
...
public TableLayout getAllAlarmList(Context con)
{
TableLayout tb = new TableLayout(con);
TableRow[] tr = new TableRow[maxCount]; //maxCount is the number of rows
CheckBox[] check = new CheckBox[maxCount]; //maxCount is the number of rows in the database.
for(int i=0;i<maxCount;i++)
{
tr[i]=new TableRow(con);
check[i]= new CheckBox(con); //con is Context class passed as argument.
check[i].setText(Integer.toString(i));
check[i].setId(100+i);
// I have to add onClick Action Listener here.
tr[i].addView(check[i]);
tb.addView(tr[i]);
}
return tb;
}
}
For this I am also keeping a track of the ids of the checkboxes.
check[i].setOnCheckedChangeListenerfor CheckBoxcheck[i].setOnCheckedChangeListener(new onCheckedChangeListener)as the class do not extend View Class