Piraba
Piraba

Reputation: 7014

Checkbox selectAll\DeselectAll in tableLayout

I have List of route in tablelayout. CheckBox & TextView created dynamically. I set the checkbox id.

I want to do the selectall & deselectAll . How we can implement?

I created checkBox using coding.then How can call this 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){

 }

enter image description here

Please help me ...

Thanks in advance..

Upvotes: 2

Views: 1539

Answers (1)

user711058
user711058

Reputation: 715

R.id.* are integer constants generated during build process. Actually, you need to pass an integer id to findViewById (same integer as you set us as id in init block). Like this:

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

But as for me this is not very reliable because id set in runtime can be not unique and I think this loop is better:

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

Upvotes: 5

Related Questions