2017-02-07 1 views
0

확인란을 선택 해제 한 후에도 배열 목록에서 값을 제거하는 두 가지 방법을 시도했지만 아직 성공하지 못한 것 같습니다.다른 체크 박스를 선택했을 때 ArrayList <String>에서 값을 제거하는 방법

아래 코드에는 각 상자에 대한 사례가 들어있는 switch 문이 포함되어 있습니다. 기본적으로 사용자가 상자 중 하나를 선택하면 ArrayList<String>에 값을 저장 한 다음 다른 방법으로 값을 저장합니다. messageTotalTip = selection.get(0);

미리 감사드립니다.

/* Create a View for our Check Boxes */ 
public void selectTip(View view){ 

    /*Boolean object will check if our checkbox is checked or not*/ 
    boolean checked =((CheckBox) view).isChecked(); 

    switch (view.getId()){ 

     /*Case if 15% is clicked*/ 
     case R.id.cb15Percent: 
      if(checked){ 

       /*store our value into the ArrayList*/ 
       selection.add("15"); 

       /*To make sure we cant click any other box*/ 
       cb18Percent.setChecked(false); 
       cb20Percent.setChecked(false); 
       cbCustomTip.setChecked(false); 

       /*Try and make sure that our value is erased if unchecked */ 
       if(cb18Percent.isChecked() || cb20Percent.isChecked()||cbCustomTip.isChecked()) { 

        selection.remove("15"); 
       } 

      } else { 
       selection.remove("15"); 
      } 
      break; 

     /*Case if 18% is clicked*/ 
     case R.id.cb18Percent: 
      if(checked){ 
       /*store our value into the ArrayList*/ 
       selection.add("18"); 

       /*Make sure we cant click any other box*/ 
       cb15Percent.setChecked(false); 
       cb20Percent.setChecked(false); 
       cbCustomTip.setChecked(false); 


       /*Try and make sure that our value is erased if unchecked */ 
       if(cb15Percent.isChecked() || cb20Percent.isChecked()||cbCustomTip.isChecked()) { 

        selection.remove("18"); 
       } 


      } else { 
       selection.remove("18"); 
      } 
      break; 

     /*Case if 20% is clicked*/ 
     case R.id.cb20Percent: 
      if(checked) { 
       /*store our value into the ArrayList*/ 
       selection.add("20"); 

       /*Make sure we cant click any other box*/ 
       cb15Percent.setChecked(false); 
       cb18Percent.setChecked(false); 
       cbCustomTip.setChecked(false); 


       /*Try and make sure that our value is erased if unchecked */ 
       if(cb18Percent.isChecked() || cb15Percent.isChecked()||cbCustomTip.isChecked()) { 
        selection.remove("20"); 
       } 

      } else { 
       selection.remove("20"); 
      } 
       break; 

     /*Case if the custom box is checked*/ /************ Add dialogue box later once smaller bugs are fixed ************/ 
     case R.id.cbCustom: 
      if(checked){ 
       selection.add("100"); 

       /*Make sure we cant click any other box*/ 
       cb15Percent.setChecked(false); 
       cb18Percent.setChecked(false); 
       cb20Percent.setChecked(false); 

       /*Try and make sure that our value is erased if unchecked */ 
       if(cb18Percent.isChecked() || cb20Percent.isChecked()||cb15Percent.isChecked()) { 

        selection.remove("100"); 
       } 
      }else { 
       selection.remove("100"); 
      } 
    } 
} 
+0

1. 언제든지 한 번만 검사 할 수 있습니까? 2. 적어도 하나는 확인해야합니까? –

+0

예, 예! 이것은 간단한 팁 계산기입니다. 팁을 계산하고 싶기 때문에 팁을 계산할 수 있도록 단 하나의 값만 가질 수 있어야합니다. – Robbie08

+0

대신 라디오 버튼을 사용하지 않는 이유는 무엇입니까? – jonhid

답변

0

업데이트 : 여러분 모두에게 큰 감사를드립니다. 내가 뭘 잘못하고 있는지 알았어. 나는 내가 ArrayList를 사용하게 만든이 글을 쓰기 전에 다른 접근법을 사용하고 있었다. 몇몇 사람들의 의견을 본 후 더 이상 ArrayList가 필요 없다는 것을 깨달았습니다.

FIX : messageTotalTip이라는 전역 변수를 설정하고 대소 문자에 따라 값을 설정하십시오.

감사합니다.

관련 문제