2016-11-22 116 views
-1

我有一個listView與複選框。當checkBox被選中/取消選中時,它將統計和顯示buttondelete setText中的數字。如果計數器等於0,則它將只顯示DELETE,否則將顯示DELETE (with the counter)。但問題是刪除靜止的setText DELETE (with the counter)雖然計數器爲0按鈕顯示覆選框計數器

holder.ckbox.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         if (((CheckBox) v).isChecked()) { 
          int getPosition = (Integer) v.getTag(); // Here we get the position that we have set for the checkbox using setTag. 
          search.get(getPosition).setSelected(((CheckBox)v).isChecked()); // Set the value of checkbox to maintain its state. 
          checkBoxCounter ++; 
          delete.setText("DELETE"+""+"("+ checkBoxCounter +")"); 
         } else 
         { 
          if(checkBoxCounter==0) 
          { 
           delete.setText("DELETE"); 
          } 
          else { 
           int getPosition = (Integer) v.getTag(); // Here we get the position that we have set for the checkbox using setTag. 
           search.get(getPosition).setSelected(((CheckBox) v).isChecked()); // Set the value of checkbox to maintain its state. 
           checkBoxCounter--; 
           delete.setText("DELETE" + "" + "(" + checkBoxCounter + ")"); 
          } 
         } 
        } 
       }); 
+0

@Kathi ya..'private靜態INT checkBoxCounter = 0; ' – Hoo

+0

@Kathi它仍然顯示DELETE(0) – Hoo

回答

1

現在你永遠檢查0更改底部else塊這樣的:

else { 
    int getPosition = (Integer) v.getTag(); // Here we get the position that we have set for the checkbox using setTag. 
    search.get(getPosition).setSelected(((CheckBox) v).isChecked()); // Set the value of checkbox to maintain its state. 
    checkBoxCounter--; 
    if (checkBoxCounter == 0) { 
     delete.setText("DELETE"); 
    else { 
     delete.setText("DELETE" + "" + "(" + checkBoxCounter + ")"); 
    } 
} 

,並刪除早期檢查

1

你的代碼改成這樣

holder.ckbox.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        if (((CheckBox) v).isChecked()) { 
         int getPosition = (Integer) v.getTag(); // Here we get the position that we have set for the checkbox using setTag. 
         search.get(getPosition).setSelected(((CheckBox)v).isChecked()); // Set the value of checkbox to maintain its state. 
         checkBoxCounter ++; 
         delete.setText("DELETE"+""+"("+ checkBoxCounter +")"); 
        } else 
        { 
         checkBoxCounter--; 
         if(checkBoxCounter==0) 
         { 
          delete.setText("DELETE"); 
         } 
         else { 
          int getPosition = (Integer) v.getTag(); // Here we get the position that we have set for the checkbox using setTag. 
          search.get(getPosition).setSelected(((CheckBox) v).isChecked()); // Set the value of checkbox to maintain its state. 
          delete.setText("DELETE" + "" + "(" + checkBoxCounter + ")"); 
         } 
        } 
       } 
      }); 

希望這對你的作品!