填充微调器

时间:2017-10-12 05:21:57

标签: java android android-spinner android-checkbox

我有4个复选框,当我检查一个,两个或更多时。我希望用值填充我的微调器,如果选中复选框1则填充1-5,如果选中复选框2则填写6-10等等。我在这里有这个逻辑。

public void populateSpinnerToothNumber() {
    if (cbQuadrant1.isChecked()) {
        ArrayList<String> toothNumber = new ArrayList<>();
        toothNumber.add("1");
        toothNumber.add("2");
        toothNumber.add("3");
        toothNumber.add("4");
        toothNumber.add("5");

        ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, toothNumber);
        spinnerToothNumber.setAdapter(stringArrayAdapter);
    } else if (cbQuadrant2.isChecked()) {

    } else if (cbQuadrant3.isChecked()) {

    } else if (cbQuadrant4.isChecked()) {

    } else if (cbQuadrant1.isChecked() && cbQuadrant2.isChecked()) {
        ArrayList<String> toothNumber = new ArrayList<>();
        toothNumber.add("1");
        toothNumber.add("2");
        toothNumber.add("3");
        toothNumber.add("4");
        toothNumber.add("5");
        toothNumber.add("6");
        toothNumber.add("7");
        toothNumber.add("8");
        toothNumber.add("9");
        toothNumber.add("10");

        ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, toothNumber);
        spinnerToothNumber.setAdapter(stringArrayAdapter);
    }
}

如何改进逻辑?

更新

public void populateSpinnerToothNumber() {
    final ArrayList<String> toothNumber = new ArrayList<>();

    cbQuadrant1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            for (int x = 1; x <= 5; x++) {
                toothNumber.add(String.valueOf(x));
            }
        }
    });

    cbQuadrant2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            for (int x = 6; x <= 10; x++) {
                toothNumber.add(String.valueOf(x));
            }
        }
    });

    cbQuadrant3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            for (int x = 11; x <= 15; x++) {
                toothNumber.add(String.valueOf(x));
            }
        }
    });

    cbQuadrant4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            for (int x = 16; x <= 20; x++) {
                toothNumber.add(String.valueOf(x));
            }
        }
    });

    ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, toothNumber);
    spinnerToothNumber.setAdapter(stringArrayAdapter);
}

这解决了问题:)用checkbox.setOnCheckedChangeListener管理它感谢您的帮助! :)

2 个答案:

答案 0 :(得分:2)

添加到@Nilesh Rathod。您可以使用一个名为private void populateSpinner (int bgCount, int Endcount) { ArrayList<String> toothNumber = new ArrayList<>(); for (int i = bgCount; i < Endcount; i++) { toothNumber.add(i); } ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, toothNumber); spinnerToothNumber.setAdapter(stringArrayAdapter); } 的方法,该方法可以获取从开始范围到结束所需的牙齿数量。

populateSpinner (1, 5);

在复选框 OnCheckedChangeListener 中调用此类方法。 Detected

答案 1 :(得分:1)

您应该使用 OnCheckedChangeListener

  

复合按钮的选中状态更改时要调用的回调的接口定义。

示例代码

 checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
           if (isChecked) {
               ArrayList<String> toothNumber = new ArrayList<>();
               toothNumber.add("1");
               toothNumber.add("2");
               toothNumber.add("3");
               toothNumber.add("4");
               toothNumber.add("5");
               ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, toothNumber);
               spinnerToothNumber.setAdapter(stringArrayAdapter);
            }
          }
        }
    );

    checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
           if (isChecked) {
                ArrayList<String> toothNumber = new ArrayList<>();
                toothNumber.add("1");
                toothNumber.add("2");
                toothNumber.add("3");
                toothNumber.add("4");
                toothNumber.add("5");
                toothNumber.add("6");
                toothNumber.add("7");
                toothNumber.add("8");
                toothNumber.add("9");
                toothNumber.add("10");
                ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, toothNumber);
                  spinnerToothNumber.setAdapter(stringArrayAdapter);
                                                }
                                            }
                                        }
    );