samedi 17 décembre 2016

How to save states of multiple dynamically generated checkboxes in a list view? (Android Java)

I have an Activity that looks like this - the length of the list view is dynamic and can change; enter image description here

Now, I have declared and initialized checkboxes based on the length of the array. I need to save the states of these boxes when the user clicks on "Save". How do I achieve this? I wrote the following code, but I get no input, i.e. even though the checkboxes are initialized, I don't think the activity knows which checkbox declaration is for which checkbox on the activity. Please help - thanks!

cbs = new CheckBox[length];
for (int i=0;i<length;i++){
        cbs[i] = new CheckBox(ThisActivity.this);
    }
btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DoOnBtnClick(v);
            }
        });
    }
    public void DoOnBtnClick (View v) {
        for(int i = 0; i < cbs.length; i++){
            if(cbs[i].isChecked()){
                selectedCheckboxes.add(toInt(cbs[i].getTag()));
                Log.e("GET TAG",Integer.toString(toInt(cbs[i].getTag())));
            }
        }
    }




Aucun commentaire:

Enregistrer un commentaire