mercredi 25 janvier 2017

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. I want to change maxSpinner depending on whether the checkbox gets selected or not.

So in the beginning uploadFile() gets called elsewhere with 5 as the argument. 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.

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




Aucun commentaire:

Enregistrer un commentaire