2017-09-28 48 views
2

我有一個spinner在我看來。根據微調值的位置,我創建了dynamic checkbox,數據通過API。現在當我改變微調值比我想要的:動態刪除視圖或覆蓋視圖Android

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(); 

然後添加新的CheckBox像你做刪除所有的子視圖(在你的情況即。複選框)。

+0

thnkx您的迴應。我正在實施與您發佈的相同的解決方案。 – Vivek