【问题标题】:Dynamic remove a view or override view Android动态删除视图或覆盖视图 Android
【发布时间】:2021-01-23 19:02:18
【问题描述】:

在我看来,我有一个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) {

            }
    });

欢迎编辑和建议。

【问题讨论】:

  • 你的dialog_checkboox_options_dynamic_school_class是什么?
  • 而且您的代码不是不言自明的,所以最好添加一些 cmets。
  • @Kunu 它是一个LinearLayout

标签: android checkbox spinner android-view


【解决方案1】:

我找到了解决方案,但可能不是最好的或不推荐(我认为)。

删除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) {

               }
           });

这至少解决了我的问题。

【讨论】:

    【解决方案2】:

    好吧,我仍然无法准确理解您想要什么。 但是通过查看您的代码和解释,我认为您希望在单击 Spinner 项时删除所有先前添加的复选框。

    所以每当你点击微调器项目时

    -首先,使用以下代码删除所有子视图(即您的情况下的复选框)

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

    然后像你一样添加新的复选框。

    【讨论】:

    • 感谢您的回复。我正在实施与您发布的相同的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 2015-05-25
    • 2013-08-18
    • 1970-01-01
    • 2017-08-03
    相关资源
    最近更新 更多