我在 Android Class (Not Activity)中动态创建 CheckBoxes 。所以我需要将onClick
Action Listener 添加到我的复选框中,如何实现这一点。
我正在使用以下代码。
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;
}
}
为此,我还跟踪复选框的 ID。