jeudi 12 août 2021

Add a checked changed listener to dinamically added checkboxes Xamarin Android

I am trying to group checkboxes so that when is checked the other is unchecked. I am adding them dinamically and as I add them I give them a click event listener but the listener is only being called on the last pair of checkboxes that were added and not on the others. So I have 6 pairs of checkboxes but only the last pair calls the click event listener. This is what I tried:

foreach (KeyValuePair<string, string> pair in parametros)
            {
                LinearLayout inner_container = new LinearLayout(this);
                inner_container.Orientation = Orientation.Vertical;
                TextView tt = new TextView(this);
                tt.Text = pair.Value;
                inner_container.AddView(tt);
                LinearLayout horizontal = new LinearLayout(this);
                horizontal.Orientation = Orientation.Horizontal;
                ckb_conforme = new CheckBox(this);
                ckb_conforme.Text = "Conforme";
                list_ckb.Add(ckb_conforme);
                list_ckb_conforme.Add(ckb_conforme);
                ckb_nao_conforme = new CheckBox(this);
                ckb_nao_conforme.Text = "Não Conforme";
                list_ckb_nao_conforme.Add(ckb_nao_conforme);

                // Make only one checkbox selected
                ckb_conforme.Click += (sender, args) =>
                {
                    if (ckb_conforme.Checked)
                        ckb_nao_conforme.Checked = !ckb_conforme.Checked;
                };
                ckb_nao_conforme.Click += (sender, args) =>
                {
                    if (ckb_nao_conforme.Checked)
                        ckb_conforme.Checked = !ckb_nao_conforme.Checked;
                };

                //ckb_conforme.SetOnCheckedChangeListener(OnCheckedChanged(ckb_conforme, false));
                //ckb_nao_conforme.SetOnCheckedChangeListener(OnCheckedChanged(ckb_nao_conforme, false));

                horizontal.AddView(ckb_conforme);
                horizontal.AddView(ckb_nao_conforme);
                inner_container.AddView(horizontal);
                params_container.AddView(inner_container);
            }

So I tried to put every checkbox in a list of checkboxes and then iterate over them and attribute them a checked changed event listener:

for (int i = 0; i < list_ckb_conforme.Count - 1; i++)
            {
                list_ckb_conforme[i].CheckedChange += (sender, args) =>
                {
                    if (list_ckb_conforme[i].Checked)
                        list_ckb_nao_conforme[i].Checked = !list_ckb_conforme[i].Checked;
                };
                list_ckb_nao_conforme[i].CheckedChange += (sender, args) =>
                {
                    if (list_ckb_nao_conforme[i].Checked)
                        list_ckb_conforme[i].Checked = !list_ckb_nao_conforme[i].Checked;
                };
            }

Didn't work either. How can I make the checkboxes group together in pairs when they are created and give them checked changed event listeners that are hit?




Aucun commentaire:

Enregistrer un commentaire