Radiobutton changes checked when scrolling Listview

Asked

Viewed 118 times

0

Good afternoon, I have a problem with Radiogroup on a listview. Next, when I mark a Radiobuttom and use the scroll of my listview, the marking I made appears in another Radiobutto and not in what I had selected. Follow the code of my Adapter:

class AdapterAmcPersonalizada extends BaseAdapter {

    private final List<AvaliacaoMensal> mensal;
    private final Activity act;

    private AvaliacaoMensal flag;


    public AdapterAmcPersonalizada(List<AvaliacaoMensal> mensal, Activity act) {
        this.mensal = mensal;
        this.act = act;


    }


    @Override
    public int getCount() {
        return mensal.size();
    }

    @Override
    public Object getItem(int position) {
        return mensal.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @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);
        RadioButton simButton = (RadioButton) view.findViewById(R.id.sim);
        RadioButton naoButton = (RadioButton) view.findViewById(R.id.nao);
        RadioButton naButton = (RadioButton) view.findViewById(R.id.na);

        //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 sim:

                        flag.radioButtonValues[0] = true;
                        flag.radioButtonValues[1] = false;
                        flag.radioButtonValues[2] = false;;

                        // trata radioValor1
                        break;
                    case nao:
                        flag.radioButtonValues[0] = false;
                        flag.radioButtonValues[1] = true;
                        flag.radioButtonValues[2] = false;
                        // trata radioValor2
                        break;
                    case na:
                        flag.radioButtonValues[0] = false;
                        flag.radioButtonValues[1] = false;
                        flag.radioButtonValues[2] = true;
                        // trata radioValor3
                        break;
                }

            }

        });



     if( flag.radioButtonValues[0] == true )
        {
            simButton.setChecked(true);
            naoButton.setChecked(false);
            naButton.setChecked(false);
        } else {
            if(flag.radioButtonValues[1] == true ){
                naoButton.setChecked(true);
                simButton.setChecked(false);
                naButton.setChecked(false);
            } else {
                if(flag.radioButtonValues[2] == true ){
                    naButton.setChecked(true);
                    simButton.setChecked(false);
                    naoButton.setChecked(false);
                }else {
                    simButton.setChecked(false);
                    naoButton.setChecked(false);
                    naButton.setChecked(false);
                }
            }
        }

In the following section, I check the position that is my listview (flag) and check the value that is within the radioButtonValues[].

   if( flag.radioButtonValues[0] == true )
        {
            simButton.setChecked(true);
            naoButton.setChecked(false);
            naButton.setChecked(false);
        } else {
            if(flag.radioButtonValues[1] == true ){
                naoButton.setChecked(true);
                simButton.setChecked(false);
                naButton.setChecked(false);
            } else {
                if(flag.radioButtonValues[2] == true ){
                    naButton.setChecked(true);
                    simButton.setChecked(false);
                    naoButton.setChecked(false);
                }else {
                    simButton.setChecked(false);
                    naoButton.setChecked(false);
                    naButton.setChecked(false);
                }
            }
        }

Class Avaliacaomensal:

 class AvaliacaoMensal {
        private String questao;
        private char potencial;
        private String titulo;
       boolean[] radioButtonValues = new boolean[3];
//Gets e sets

But it’s not working as expected, because the dialing is going to another Radiogroup, someone can help me?

  • Last if, insert a else { naButton.setChecked(false); }

  • @acklay Pq? do not understand.

  • I’m seeing that without the true but no false. You have to create a denial too if it’s not true. Then at the end you are creating an if without Else. Try adding Else if it is not true. if(flag.radioButtonValues[2] == true ){&#xA; naButton.setChecked(true);&#xA;} else { naButton.setChecked(false); }

  • @acklay So at last This I would have to put }else {&#xA; simButton.setChecked(false);&#xA; naoButton.setChecked(false);&#xA; naButton.setChecked(false);&#xA; } But it hasn’t changed at all =[

  • That’s right Lucas... I’m asking you to do it like this to see if it works! = D

  • @acklay Yes yes it makes sense. But it did not give no :(

  • It also has the simButton button that you have to put simButton.setChecked(false) at some point.

  • Your logic is very strange. hehe

  • @acklay Wow ... I check in Flag position if certain radiobutton is marked, in case I’m setCheked on it. It was the best I could think of :(

  • No need to compare if it is true, just do if(flag.radioButtonValues[0]){ ... bla bla bla}.... That already means it’s true. Look at the ramaral’s answer, maybe it solves your problem. Exactly what I was trying to say.

  • @acklay sorry I responded, but I was already doing it when I saw your comments.

  • @ramaral Relax! The main goal of the OS is to work together to help others! I think we are fulfilling this. = D

  • 2

    @acklay I know, but I don’t like to meddle (I don’t see the need) when someone is already helping and is on the right track.

  • 1

    When editing the question with updated code with that of the answer you leave the answer meaningless. Whoever reads the question now will say that I to answer copied the question code.

  • @Lucascharles It’s not cool for you to edit a question by inserting answer content from people who tried to help you. This ends up invalidating the answers. The interesting thing is to talk and try to solve from what you already have.

  • What’s in the Evaluator class? You can show her content?!

  • @Okay acklay, I edited my question. :)

  • 1

    Why are you using the flag = mensal.get(position); if you already have the ending AvaliacaoMensal mensalAmc = mensal.get(position);. I shouldn’t be comparing the mensalAmc

  • @acklay my flag was picking up random values. I just don’t know pq. THANK YOU!

Show 14 more comments
No answers

Browser other questions tagged

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