如何检查复选框后的标签?

时间:2022-06-15 15:06:16

I've tried this:

我试过这个:

private int[] intCheckBox = {
        R.id.monCheck,
        R.id.tueCheck,
        R.id.wedCheck
};

private CheckBox[] realCheckBox = new CheckBox[intCheckBox.length];

private int i;

Then:

 for (i=0;i<intCheckBox.length;i++){
        realCheckBox[i] = (CheckBox) findViewById(intCheckBox[i]);
        realCheckBox[i].setTag(i);
        realCheckBox[i].setOnCheckedChangeListener(showFinal);
    }

}

private CompoundButton.OnCheckedChangeListener showFinal = new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        String outPut2 = "";

        if (realCheckBox[i].isChecked()){
            outPut2 += realCheckBox[i].getTag();
            outPutText3.setText(outPut2);

        }

However, when i ran the app, there was an error shown after i had checked one of the boxes, and the error was:

但是,当我运行应用程序时,我检查了其中一个框后显示错误,错误是:

E/AndroidRuntime: FATAL EXCEPTION: main
              java.lang.ArrayIndexOutOfBoundsException: length=3; index=3

I know there is another way to get the tag or even the string by setting them one by one... Yet, i would like to use the array to show the tag in order to do the further coding that to get the int value of "whatIReallyWantToGet":

我知道有另一种方法来获取标签甚至字符串通过逐个设置...然而,我想使用数组来显示标签,以便进行进一步的编码,以获得int值“whatIReallyWantToGet”:

 java.util.Calendar cal = java.util.Calendar.getInstance();
        cal.set(Calendar.MONTH, position);
        cal.set(Calendar.DAY_OF_WEEK, whatIReallyWantToGet);
        cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1);
        int month = cal.get(Calendar.MONTH);
        String output = "";
        while (cal.get(Calendar.MONTH) == month) {
            output += cal.get(Calendar.DAY_OF_MONTH) + "/" + (cal.get(Calendar.MONTH) + 1) + ",";
            cal.add(Calendar.DAY_OF_MONTH, 7);
        }

I am searching for a long time on net. But no use. I'm just a new learner from textbooks and online resources. Please help or try to give some ideas how to achieve this. Thank you so much!

我在网上搜索了很长时间。但没用。我只是一个来自教科书和在线资源的新学习者。请帮助或尝试提供一些如何实现这一目标的想法。非常感谢!

1 个解决方案

#1


0  

From the code you shared, it seems to me that you are not resetting the value of the variable i.

从您共享的代码中,在我看来,您没有重置变量i的值。

That would explain why the exception says you are trying to access the index 3 of the array

这可以解释为什么异常说您正在尝试访问数组的索引3

E/AndroidRuntime: FATAL EXCEPTION: main java.lang.ArrayIndexOutOfBoundsException: length=3; index=3

E / AndroidRuntime:FATAL EXCEPTION:main java.lang.ArrayIndexOutOfBoundsException:length = 3;索引= 3

#1


0  

From the code you shared, it seems to me that you are not resetting the value of the variable i.

从您共享的代码中,在我看来,您没有重置变量i的值。

That would explain why the exception says you are trying to access the index 3 of the array

这可以解释为什么异常说您正在尝试访问数组的索引3

E/AndroidRuntime: FATAL EXCEPTION: main java.lang.ArrayIndexOutOfBoundsException: length=3; index=3

E / AndroidRuntime:FATAL EXCEPTION:main java.lang.ArrayIndexOutOfBoundsException:length = 3;索引= 3