java - 填充微调器

标签 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 管理它 感谢您的帮助! :)

最佳答案

添加到@Nilesh Rathod。您可以使用名为 populateSpinner (int bgCount, int Endcount) 的方法,该方法获取从开始范围到结束所需的 dentry 数量。

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

在复选框中调用这样的方法OnCheckedChangeListenerpopulateSpinner(1, 5);

关于java - 填充微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46701971/

相关文章:

java - Maven:如何从命令行更改目标目录的路径?

java - 链接多个 Transformations.switchMap 关闭一个 LiveData 源

java - 基于 SQLite 数据库数据创建带有文本和 Collection 夹图标的 ListView

android - 我能否判断用户是否已接受我的 Activity 的屏幕固定请求?

java - 如何在点击按钮时重置 Android 应用程序?

android - 更改微调器背景会生成深色边框

Android 水平 ScrollView ,具有类似旋转器的功能

Java邮件API : Unable to connect to host

java - Android OpenGL ES 1 : Filling display with a quad

android - 以 xml 或以编程方式为 spinner 提供数组?