checkbox selectAll\DeselectAll in tableLayout

Pir*_*aba 2 android

我有tablelayout中的路由列表.CheckBox和TextView动态创建.我设置了复选框ID.

我想做selectall&deselectAll.我们如何实施?

我使用coding.then创建了checkBox.如何调用它 CheckBox cb = (CheckBox)view.findViewById(R.id.????);

 tl = (TableLayout) findViewById(R.id.dailyDRoute);
    ArrayList<SalesRoutes> routeList = getSalesRoute();
    for (int i = 0; i < routeList.size(); i++) {
        TableRow tr = new TableRow(this);
        CheckBox ch = new CheckBox(this);
        ch.setHeight(1);
        ch.setId(i);
        TextView tv2 = new TextView(this);
        tr.addView(ch);
        tv2.setText(routeList.get(i).getDescription());
        tv2.setTextColor(Color.BLACK);

        tr.addView(tv2); 
        tl.addView(tr);
    }
}


 public void deselectAll(View view){
    // CheckBox cb = (CheckBox)view.findViewById(R.id.);

 }


 public void selectAll(View view){

 }
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

请帮我 ...

提前致谢..

use*_*058 5

R.id.*是构建过程中生成的整数常量.实际上,您需要传递一个整数id findViewById(与在init块中将我们设置为id 的整数相同).像这样:

for (int i = 0; i < tl.getChildCount(); i++) {
  CheckBox cb = (CheckBox)view.findViewById(i);
  cb.setChecked(true);
}       
Run Code Online (Sandbox Code Playgroud)

但对我来说这不是很可靠,因为在运行时设置的id可能不是唯一的,我认为这个循环更好:

for (int i = 0; i < tl.getChildCount(); i++) {
  CheckBox cb = (CheckBox)((TableRow)tl.getChildAt(i)).getChildAt(0);
  cb.setChecked(true);
}
Run Code Online (Sandbox Code Playgroud)