如何取消选中新的微调框值复选框? [英] How do I uncheck checkbox upon new spinner value?

查看:70
本文介绍了如何取消选中新的微调框值复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说说vocSpinner值=战斗/服务"和popSpinner值="NSmen",然后检查cb1.它将给出一个列表.但是,当我将popSpinner更改为"Regular/NSF"时,它将更改为另一个列表(正确),但是如何取消选中该复选框,而又不创建另一个覆盖的数据.我提供的代码似乎无效,我也无法解决.欢迎所有帮助,在此先感谢!

Let's say for vocSpinner value = "Combat/Service" and popSpinner value = "NSmen" and I check cb1. It will give a list. However, when I change popSpinner to "Regular/NSF" it will change to another list (which is correct) but how do I uncheck the checkbox as well without creating another data that overlay. The codes I provided doesn't seem to work and I can't work it out. All help are welcome, thanks in advance!

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

imgur.com/a/1wt4N [Screenshot of app]

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

推荐答案

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

in your onItemSelected listener of your spinner you have done

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

以下,您也应该这样做

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

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

also if no checkbox is checked, it is manually selecting the same values of your spinner that were previously selected so that your selection listener of spinner is called and your array is repopulated based on spinner values. For that do the following in your else -

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看起来像

So your one if condition onCheckedChanged would look like

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

这篇关于如何取消选中新的微调框值复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆