2017-09-28 9 views
2

나는 내 생각에 spinner을 가지고있다. 회 전자 값 위치에 따라 dynamic checkbox을 만들고 데이터는 API을 통해옵니다. 이제 내가 원하는 것보다 Spinner 값을 변경할 때 :동적 뷰를 삭제하거나보기를 덮어 쓴다.

Hide previous checkboxcreate new OR override previous checkbox에 새 값이 변경되었습니다.

지금 작성 동적 확인란을 만들 수 있으며 회 전자 값을 변경할 때 현재 상자에 새 확인란을 추가합니다. 나는 hide/remove/override 그들을 기울인다.

otherSchool.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
       if (position == 0) { 
        if (allSchoolClassesName.size() > 0) { 
         for (int j = 0; j < allSchoolClassesName.size(); j++) { 
          final CheckBox addonChechbox = new CheckBox(context); 
          addonChechbox.setText(""); 
          dialog_checkboox_options_dynamic_school_class.removeView(addonChechbox); 
          addonChechbox.setText(allSchoolClassesName.get(j)); 
          addonChechbox.setId(j); 
          addonChechbox.setTextColor(context.getResources().getColor(R.color.White)); 
          addonChechbox.setButtonDrawable(context.getResources().getDrawable(R.drawable.selector_checkbox)); 
          addonChechbox.setPadding(0, 0, 0, 0); 
          addonChechbox.setTextColor(context.getResources().getColor(R.color.black)); 
          addonChechbox.setTypeface(tf); 

          for (int groupIds = 0; groupIds < groupsArray.size(); groupIds++) { 
           if (allSchoolClassesId.get(j).equalsIgnoreCase(groupsArray.get(groupIds))) { 
            addonChechbox.setChecked(true); 
            addonChechbox.setButtonDrawable(context.getResources().getDrawable(R.drawable.selector_checkbox)); 
            selectedGroupId.add(allSchoolClassesId.get(addonChechbox.getId())); 
            selectedGroupType.add("1"); 
           } 
          } 
          dialog_checkboox_options_dynamic_school_class.addView(addonChechbox); 
          addonChechbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
           @Override 
           public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 

             if (isChecked) { 
              selectedGroupId.add(allSchoolClassesId.get(addonChechbox.getId())); 
              selectedGroupType.add("1"); 
             } else { 
              boolean findSelectedId = selectedGroupId.contains(allSchoolClassesId.get(addonChechbox.getId())); 
              if (findSelectedId) { 
               int indexSelectedId = selectedGroupId.indexOf(allSchoolClassesId.get(addonChechbox.getId())); 
               selectedGroupId.remove(indexSelectedId); 
               selectedGroupType.remove(indexSelectedId); 
              } 
             } 
           } 
          }); 
         } 
        } 
       } else if (position > 0) { 
        if (schData.length() > 0) { 
         int i = 1; 
         int pos = position; 
         for (int k = 0; schData.length() > 0; k++) { 
          if (position == i) { 
           JSONObject achArray = schData.optJSONObject(k); 
           JSONArray grpList = achArray.optJSONArray("grpList"); 
           for (int j = 0; j < grpList.length(); j++) { 
            final CheckBox addonChechbox = new CheckBox(context); 
            addonChechbox.setText(""); 
            dialog_checkboox_options_dynamic_school_class.removeView(addonChechbox); 
            JSONObject classObj = grpList.optJSONObject(j); 
            String classes = classObj.optString("classes"); 
            JSONObject jsonObjectId = classObj.optJSONObject(Constants.CONSTANT_id); 
            final String classGroupId = jsonObjectId.optString(Constants.CONSTANT_$id); 
            addonChechbox.setText(""); 
            addonChechbox.setText(classes); 
            addonChechbox.setId(j); 
            addonChechbox.setTextColor(context.getResources().getColor(R.color.White)); 
            addonChechbox.setButtonDrawable(context.getResources().getDrawable(R.drawable.selector_checkbox)); 
            addonChechbox.setPadding(0, 0, 0, 0); 
            addonChechbox.setTextColor(context.getResources().getColor(R.color.black)); 
            addonChechbox.setTypeface(tf); 

            for (int groupIds = 0; groupIds < groupsArray.size(); groupIds++) { 
              if (classGroupId.equalsIgnoreCase(groupsArray.get(groupIds))) { 
                addonChechbox.setChecked(true); 
                addonChechbox.setButtonDrawable(context.getResources().getDrawable(R.drawable.selector_checkbox)); 
                selectedGroupId.add(classGroupId); 
                selectedGroupType.add("1"); 
              } 
            } 
            dialog_checkboox_options_dynamic_school_class.removeView(addonChechbox); 
            dialog_checkboox_options_dynamic_school_class.addView(addonChechbox); 
            addonChechbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
             @Override 
             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 

              if (isChecked) { 
               selectedGroupId.add(classGroupId); 
               selectedGroupType.add("1"); 
              } else { 
               boolean findSelectedId = selectedGroupId.contains(classGroupId); 
               if (findSelectedId) { 
                int indexSelectedId = selectedGroupId.indexOf(classGroupId); 
                selectedGroupId.remove(indexSelectedId); 
                selectedGroupType.remove(indexSelectedId); 
               } 
              } 
             } 
            }); 
           } 
           break; 
          } 
           i++; 
         } 
        } 
       } 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> parent) { 

      } 
    }); 

편집 및 제안을 환영합니다 :

여기 내 코드입니다.

+0

'dialog_checkboox_options_dynamic_school_class'란 무엇입니까? – Kunu

+0

그리고 코드가 자명하지 않으므로 주석을 추가하는 것이 좋습니다. – Kunu

+0

@Kunu는 그것의'LinearLayout'입니다. 귀하의 답변은 – Vivek

답변

3

해결책을 찾았지만 좋지 않거나 권장하지 않을 수 있습니다 (제 생각에는).

LinearLayout의 모든 내용을 제거하십시오.

  otherSchool.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
       if (position == 0) { 
      dialog_checkboox_options_dynamic_school_class.removeAllViews(); 
       } else if (position > 0) { 
        if (schData.length() > 0) { 
           dialog_checkboox_options_dynamic_school_class.removeAllViews(); 
       } 
       } 
      @Override 
      public void onNothingSelected(AdapterView<?> parent) { 

       } 
      }); 

이렇게하면 내 문제가 적어도 해결됩니다.

1

글쎄, 나는 네가 정확히 원하는 것을 이해할 수 없다. 하지만 Spinner 항목을 클릭 할 때 이전에 추가 한 모든 CheckBox를 제거하려고한다고 생각하는 코드와 설명을 살펴보십시오.

그래서 당신은 회 전자 항목

- 첫째을 클릭 할 때마다, 그리고 당신이하고 같은 새로운 체크 박스를 추가

if(((LinearLayout) dialog_checkboox_options_dynamic_school_class).getChildCount() > 0) 
     ((LinearLayout) dialog_checkboox_options_dynamic_school_class).removeAllViews(); 

코드

이하로 사용하여 모든 아이 뷰 (귀하의 경우 즉. 체크 박스)를 제거합니다.

+0

thnkx입니다. 나는 u로 게시 된 동일한 솔루션을 구현했다. – Vivek