jeudi 28 septembre 2017

Dynamic remove a view or override view Android

I have a spinner in my view. Based on spinner value position I am creating dynamic checkbox and data is coming through API. Now when I change Spinner value than I want :

Hide previous checkbox and create new OR override previous checkbox with new one.

Write now I am able to create dynamic checkbox, and when I change spinner value It add new checkbox with current boxes. I cant hide/remove/override them.

Here is my code:

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

            }
    });

Editing and suggestion are welcome.




Aucun commentaire:

Enregistrer un commentaire