如何在新的微调器值上取消选中复选框?

时间:2017-07-26 08:48:15

标签: android checkbox android-recyclerview

让我们说vocSpinner值="战斗/服务"和popSpinner值=" NSmen"我检查cb1。它会给出一个清单。但是,当我将popSpinner更改为" Regular / NSF"它将更改为另一个列表(这是正确的),但如何取消选中该复选框,而不创建其他重叠数据。我提供的代码似乎没有用,我无法解决。欢迎所有的帮助,提前谢谢!

imgur.com/a/1wt4N [应用截图]

private String[] vocSpinner;
private String[] popSpinner;

private List<Standard> standardList = new ArrayList<>();
private RecyclerView recyclerView;
private StandardsAdapter sAdapter;
private CheckBox cb1;
private CheckBox cb2;

cb1 = (CheckBox) getActivity().findViewById(R.id.pushUp);
cb2 = (CheckBox) getActivity().findViewById(R.id.sitUp);

this.vocSpinner = new String[]{
            "CDO/Diver/Gds/Fitness Spec", "Combat/Service"
    };

this.popSpinner = new String[]{
            "NSmen", "Regular/NSF", "Pre-enlistee"
    };

final Spinner v = (Spinner) getActivity().findViewById(R.id.spinner_Voc);
final Spinner p = (Spinner) getActivity().findViewById(R.id.spinner_PopGp);

ArrayAdapter<String> adapterV = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_spinner_item, vocSpinner);
    v.setAdapter(adapterV);

ArrayAdapter<String> adapterP = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_spinner_item, popSpinner);
    p.setAdapter(adapterP);

v.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            standardList.clear();
            cb1.setEnabled(true);
            cb2.setEnabled(true);
            switch (position) {
                case 0:
                    if (p.getSelectedItem().equals("NSmen")) {
                        cb1.setChecked(false);
                        cb2.setChecked(false);
                        addStandardToList("Gold - $500", ">89pts");
                        addStandardToList("Silver - $300", ">74pts");
                        addStandardToList("Incentive - $200", ">60pts");
                        addStandardToList("Pass", ">50pts");
                        sAdapter.notifyDataSetChanged();} 
                    else if (p.getSelectedItem().equals("Regular/NSF")) {
                        cb1.setChecked(false);
                        cb2.setChecked(false);
                        addStandardToList("Gold - $300", ">89pts");
                        addStandardToList("Silver - $200", ">74pts");
                        addStandardToList("Incentive", "NA");
                        addStandardToList("Pass", ">60pts");
                        sAdapter.notifyDataSetChanged();

2 个答案:

答案 0 :(得分:0)

从以下代码中获取cb:

View myView = list.getChildAt(index);
CheckBox checkItem = (CheckBox) myView.findViewById(R.id.checkboxId);
checkItem.setChecked(false);

答案 1 :(得分:0)

在您的微调器的onItemSelected侦听器中

cb1.setEnabled(true); 
cb2.setEnabled(true); 

在下面你也应该

cb1.setChecked(false); 
cb2.setChecked(false); 

如果未选中任何复选框,则会手动选择先前选择的微调器的相同值,以便调用微调器的选择侦听器,并根据微调器值重新填充数组。为此,请在其他地方执行以下操作 -

String getSelectedItemP = p.getSelectedItem().toString();
                    p.setSelection(((ArrayAdapter<String>)p.getAdapter()).getPosition(getSelectedItemP));
                    String getSelectedItemV = v.getSelectedItem().toString();
                    v.setSelection(((ArrayAdapter<String>)p.getAdapter()).getPosition(getSelectedItemV));

所以你的那个如果条件onCheckedChanged看起来像

 if (v.getSelectedItem().equals("CDO/Diver/Gds/Fitness Spec") || v.getSelectedItem().equals("Combat/Service") && p.getSelectedItem().equals("NSmen")) {
                if (cb1.isChecked() && cb2.isChecked()) {
                    addStandardToList("Gold - $500", "NA");
                    addStandardToList("Silver - $300", "NA");
                    addStandardToList("Incentive - $200", "NA");
                    addStandardToList("Pass", ">25pts");
                    sAdapter.notifyDataSetChanged();
                } else if (cb1.isChecked() || cb2.isChecked()) {
                    addStandardToList("Gold - $500", "NA");
                addStandardToList("Silver - $300", "NA");
                addStandardToList("Incentive - $200", "NA");
                addStandardToList("Pass", ">38pts");
                sAdapter.notifyDataSetChanged();
                }else{
                    // select spinner again here manually.
                    String getSelectedItemP = p.getSelectedItem().toString();
                    p.setSelection(((ArrayAdapter<String>)p.getAdapter()).getPosition(getSelectedItemP));
                    String getSelectedItemV = v.getSelectedItem().toString();
                    v.setSelection(((ArrayAdapter<String>)p.getAdapter()).getPosition(getSelectedItemV));
                }
            }