2011-08-18 6 views
2

나는 경로 목록을 테이블 레이아웃에 가지고 있습니다. CheckBox & TextView가 동적으로 생성되었습니다. 체크 박스 ID를 설정했습니다.checkbox selectAll DeselectAll in tableLayout

내가 선택하고 싶습니다 & 선택을 취소하십시오. 어떻게 구현할 수 있습니까?

나는 ...이 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

제발 도와주세요 호출하는 방법을 사전에

감사합니다 ..

답변

5

R.id을 coding.then 사용하여 체크 박스를 생성 . *는 빌드 프로세스 중에 생성 된 정수 상수입니다. 실제로, 정수 id를 findViewById (init 블록에서 id로 설정 한 것과 같은 정수)에 전달해야합니다. 이처럼 :

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

그러나 런타임에서 ID를 설정하지 고유 수와 나는이 루프를 더 생각하기 때문에 이것은 매우 신뢰할 수 없습니다 나에게로 :

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

덕분에 그 작업을 잘 ... . – Piraba