Ryan Ryan - 3 years ago 66
Android Question

How do I uncheck checkbox upon new spinner value?

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

Answer Source

in your onItemSelected listener of your spinner you have done

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

Below that you should also do

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

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

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));
                }
            }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download