2017-11-04 2 views
0

버튼이 Select All입니다. 클릭하면 listview에있는 check boxes이 모두 확인되어야합니다. 동일한 버튼으로 텍스트를 Clear All으로 변경합니다. 클릭하면 모든 체크 박스가 선택 취소되어 텍스트가 Select All ... (으)로 되돌아갑니다. ...select all/clear all 버튼을 선택하고 체크 표시를하지 않으려면

그러나 listview에서는 두 번째 버튼이 선택되거나 선택 해제됩니다. Select All을 클릭하면

checkbox 1, 35이 확인된다. Checkbox 24이 선택 취소됩니다. 텍스트가 Clear All으로 바뀝니다.

Clear All을 클릭하면 checkbox 1, 35이 선택 취소됩니다. Checkbox 24이 확인됩니다. 텍스트는 Select All으로 바뀝니다.

내 코드에 어떤 문제가 있는지 알 수 없습니다. 또는 어쩌면 내 앱의 다른 부분에있는 것이지만 오류나 경고가 없으며 디버깅을 시작할 위치가 확실하지 않습니다. 감사.

if(btnCheckAll.getText().toString().equalsIgnoreCase("Select All")) { 
       checkbox.setChecked(true); 
       btnCheckAll.setText("Clear All"); 
}else if (btnCheckAll.getText().toString().equalsIgnoreCase("Clear All")){ 
         checkbox.setChecked(false); 
       btnCheckAll.setText("Select All"); 
} 

if 루프 내부에, 그래서 각 루프에 SWICH는 turining로 끌 역할 :

btnCheckAll.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      int count = MatchingContactsAsArrayList.size(); 
      for (int i = 0; i < count; i++) { 
      LinearLayout itemLayout = (LinearLayout) listView.getChildAt(i); // Find by under LinearLayout 
      CheckBox checkbox = (CheckBox) itemLayout.findViewById(R.id.checkBoxContact); 


      if(btnCheckAll.getText().toString().equalsIgnoreCase("Select All")) { 
       checkbox.setChecked(true); 
       btnCheckAll.setText("Clear All"); 
      } 


       else if (btnCheckAll.getText().toString().equalsIgnoreCase("Clear All")){ 
         checkbox.setChecked(false); 
       btnCheckAll.setText("Select All"); 


      }}}}); 

답변

1

루프의 첫 번째 루프는 두 번째 루프는 다시 변경 텍스트를 변경하고이 부분에서 변화되는에 유지하기 때문에 truefalse에서 변경에 계속 내부의 조건 (코멘트를 읽어!) :

if(btnCheckAll.getText().toString().equalsIgnoreCase("Select All")) {//This condition will be true on first round and 3 and 5 and 7... 
      checkbox.setChecked(true); 
      btnCheckAll.setText("Clear All"); 
     } 


      else if (btnCheckAll.getText().toString().equalsIgnoreCase("Clear All")){//And this one will be true in 2 and 4 and 6... 
        checkbox.setChecked(false); 
      btnCheckAll.setText("Select All"); 

해결책 :

btnCheckAll.setOnClickListener(new View.OnClickListener() { 

    public void onClick(View v) { 
     boolean toCheck=true; 

     //THE CONDITION SHOULD BE OUTSIDE THE LOOP! 

if(btnCheckAll.getText().toString().equalsIgnoreCase("Select All")) { 
      toCheck=true; 
      btnCheckAll.setText("Clear All"); 
     } 


      else if (btnCheckAll.getText().toString().equalsIgnoreCase("Clear All")){ 
        toCheck=false; 
      btnCheckAll.setText("Select All"); 


     } 



     int count = MatchingContactsAsArrayList.size(); 
     for (int i = 0; i < count; i++) { 
     LinearLayout itemLayout = (LinearLayout) listView.getChildAt(i); // Find by under LinearLayout 
     CheckBox checkbox = (CheckBox) itemLayout.findViewById(R.id.checkBoxContact); 
     checkbox.setChecked(toCheck); 
    }} 
}); 
1

나는 문제가 여기에있을 것 같아요.

각 루프에서 "모두 선택"/ "모두 지우기"텍스트가 변경되므로 다음 루프에서 버튼 상태를 평가합니다.

관련 문제