jeudi 4 mai 2017

Best solution for Checkbox in LinearLayout

Hey i have a problem,

in my LinearLayout are a variable number of CheckBoxes. In a question i had a month ago someone said it´s better to add checkboxes dynamicly instead of make them not visible.

  Integer[] count = new Integer[]{1,2,3,4,5,6,7,8,9,10};
        size = mFrageList.get(position).getAuswahlList().size();
        for (int i = 0; i < size; i++) {

            cBox = new CheckBox(this);
            cBox.setText(mFrageList.get(position).getAuswahlList().get(i));
            cBox.setId(count[i]);
            cBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    if(isChecked){
                        antwortencode[position] += "" + buttonView.getId();
                        frageBeantworten.setText("Antwort :"+antwortencode[position]+" abgeben");

                    } else {
                        String id = Integer.toString(buttonView.getId());
                        antwortencode[position] = antwortencode[position].replaceAll(id,"");
                        if(!antwortencode[position].isEmpty() || antwortencode[position]!= "") {
                            frageBeantworten.setText("Antwort :" + antwortencode[position] + " abgeben");
                        } else {
                            frageBeantworten.setText("Keine Checkbox(en) gewählt");
                        }
                    }
                }
            });

            antworten.addView(cBox);

What is possible atm: Im able to safe a String with the checked checkboxes, if i uncheck a checkbox it delets his value out of the String. If i update the activity the string is safed, and the checkboxes get a new List from the mFrageList.get(position)getAuswahlList(); and fill a new String in the "antwortencode" List with their values.

If i go back to the last position i have the String which was generated but the checkboxes aren´t checked anymore. But they have the Strings from the old position. that means everything is safed exept the state of the checkboxes. I cant set a cBox.setChecked(isChecked) or buttonView.setChecked(isChecked) or buttonView.setChecked(buttonView.isChecked()) or something which is nearly the same in syntax. If anyone could help me, it would be really nice. Cause i have no idea what i can do next instead of declaring 10 Checkboxes in a xml file to talk to them one bye one and set the VISIBLE.false if the auswahlList.get(position).isEmpty().

IMPORTANT: My XML is a Scrollable Activity cause the size of the content overextendet the screen. Thats why i didn´t and can´t use a Listview. So i need the solution with LinearLayout :D




Aucun commentaire:

Enregistrer un commentaire