Cannot Resolve Method isChecked()

Asked

Viewed 245 times

1

I am trying to use the isChecked() method but is giving the message "Cannot Resolve Method Ischeked(). Can anyone help me?

  @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = act.getLayoutInflater().inflate(R.layout.activity_layout_lista_amc, parent, false);


        final AvaliacaoMensal mensalAmc = mensal.get(position);

        //pegando as referências das Views
        TextView potencial = (TextView) view.findViewById(R.id.potencialLetra);
        TextView questao = (TextView) view.findViewById(R.id.questao);
        TextView titulo = (TextView) view.findViewById(R.id.titulo);

        //populando as Views
        potencial.setText(String.valueOf(mensalAmc.getPotencial()));
        questao.setText(String.valueOf(mensalAmc.getQuestao()));
        titulo.setText(String.valueOf(mensalAmc.getTitulo()));

        RadioGroup radioGroupAmc = (RadioGroup) view.findViewById(R.id.radioGroupAmc);
        flag = mensal.get(position);

        radioGroupAmc.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                switch(checkedId) {
                    case R.id.sim:
                        flag.radioButtonValues[0] = group.isChecked();
                        // trata radioValor1
                        break;
                     case nao:
                       flag.radioButtonValues[1] = group.isChecked();
                        // trata radioValor2
                        break;
                    case na:
                        flag.radioButtonValues[2] = group.isChecked();
                        // trata radioValor3
                        break;
                }

            }
        });

        return view;
    }


}
  • What do you intend to do?

  • @ramaral Save if radiobutton was selected or not

2 answers

2


This method does not exist in the Radiogroup class.
The Radiobutton that was selected is the one whose id is in checkedId. That switch already identifies which is.

If you want to keep in flag will have to do so:

switch(checkedId) {
    case R.id.sim:
        flag.radioButtonValues[0] = true;
        flag.radioButtonValues[1] = false;
        flag.radioButtonValues[2] = false;
        // trata radioValor1
        break;
     case R.id.nao:
        flag.radioButtonValues[0] = false;
        flag.radioButtonValues[1] = true;
        flag.radioButtonValues[2] = false;
        // trata radioValor2
        break;
    case R.id.na:
        flag.radioButtonValues[0] = false;
        flag.radioButtonValues[1] = false;
        flag.radioButtonValues[2] = true;
        // trata radioValor3
        break;
}

A Radiogroup can only have a selected Radiobutton when assigning true to a position of array shall assign false to the others.
There is way to do this in a "more elegant" way but in the absence of more information is what I can suggest.

1

First you need to instantiate the RadioButton, not only the RadioGroup.

A solution with a RadioGroup with three RadioButton.

RadioGroup group = (RadioGroup) view.findViewById(R.id.radio);
final RadioButton[] rb = {
        (RadioButton) view.findViewById(R.id.sim),
        (RadioButton) view.findViewById(R.id.nao),
        (RadioButton) view.findViewById(R.id.na)
};
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
            switch (checkedId) {
                case R.id.sim :
                    flag.radioButtonValues[0] = rb[0].isChecked();
                    break;
                case R.id.nao :
                    flag.radioButtonValues[1] = rb[1].isChecked();
                    break;
                case R.id.na :
                    flag.radioButtonValues[3] = rb[2].isChecked();
                    break;
            }
        }
    });

The RadioButton does not have the method isChecked(), as stated in the reference to RadioButton here, even because the group has several buttons, so there is no way to know if the whole group is selected, would be a bit confused until there was this possibility.

There with the RadioButton instantiated you can know if it is checked or not with the method idChecked().

I imagine it’s less gambiarra that way.

It can be improved, but that’s the intention.

  • Thank you very much!

  • Problem-free =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.