简体   繁体   English

单击复选框刷新和更改布局

[英]refresh and alter layout on click of checkbox

I didn't really know how to phrase this. 我真的不知道该怎么说。 Basically I have this method: 基本上我有这种方法:

private void uploadFile(int maxSpinner) {
    setContentView(R.layout.upload);

    CheckBox local = (CheckBox) findViewById(R.id.local_checkbox);
    Spinner dropdown = (Spinner)findViewById(R.id.spinner1);

    local.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            checkbox = isChecked;

            if( isChecked){

                uploadFile(6);
                Log.i("checked", "yes");

            }
            else {
                uploadFile(5);
                Log.i("checked", "no");
            }
        }
    });

    ArrayList<String> categories = new ArrayList<>();
    for( int category = 1; category <= maxSpinner+1; category++){
        categories.add(Integer.toString(category));
    }
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, categories);
    dropdown.setAdapter(adapter);
    dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                                   int arg2, long arg3) {
            category = arg2+1;
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });
}

The corresponding layout file has a checkbox and a spinner. 相应的布局文件具有一个复选框和一个微调器。

maxSpinner is the limit for the for-loop that sets the amount of Spinner elements. maxSpinner是用于设置Spinner元素数量的for循环的限制。 I want to change maxSpinner depending on whether the checkbox gets selected or not. 我想根据复选框是否被选中来更改maxSpinner

So in the beginning uploadFile() gets called elsewhere with 5 as the argument. 因此,在开始时,在其他地方以5作为参数调用uploadFile() When I click the checkbox local I want to call uploadFile again, but this time with either 6 or again 5 as an argument, so I get a different amount of elements for the spinner. 当我单击local复选框时,我想再次调用uploadFile ,但是这次使用65作为参数,因此我得到了微调框不同数量的元素。

The way I tried it is seen above. 上面显示了我尝试过的方式。 Nothing happens when I click the checkbox, I can see in the log it always logs "yes", even though the checkbox never actually gets visibly checked. 当我单击该复选框时,什么也没有发生,即使该复选框从未真正被明显地选中,我仍可以在日志中看到它始终记录为“是”。

any help would be appreciated, 任何帮助,将不胜感激,

thanks 谢谢

抱歉,它确实按预期运行,只是没有更新复选框,因为我没有告诉它这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM