RadioGroup in ListView does not maintain selection after scroll

1

I have a Listview with some information and within them have a RadioGroup, but when I roll all my screen checkbox is unchecked coz the listview is creating these checkbox again. I need to have my checkboxes tick when scrolling the page.

Here is the code for my adapter from this lisview:

class AdapterAmcPersonalizada extends BaseAdapter {

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


        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);
            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);
           radioGroupAmc.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {


                    switch(checkedId) {
                        case R.id.sim:

                                // trata radioValor1
                                break;
                        case R.id.nao:

                                // trata radioValor2
                                break;
                        case R.id.na:

                            // trata radioValor3
                            break;
                    }

                }
            });



            return view;
        }



    }

Here is the layout code for my listview with RadioGroup:

    <TextView
        android:id="@+id/potencial"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:paddingRight="3dp"
        android:paddingTop="3dp"
        android:text="Potencial"
        android:textAllCaps="true"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/potencialLetra"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="25dp"
        android:text="A"
        android:textAllCaps="true"
        android:textColor="@color/azulgerdau"
        android:textSize="40sp"
        android:layout_below="@+id/potencial"
        android:layout_alignParentStart="true"
        android:layout_toStartOf="@+id/questao" />

    <TextView
        android:id="@+id/questao"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/potencial"
        android:text="As máquinas e equipamentos possuem selo de liberação por um líder Gerdau e está dentro do prazo de validade?"
        android:textAlignment="center"
        android:textSize="16sp" />

<TextView
    android:id="@+id/titulo"
    android:textAlignment="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Condiçao Fisica"
    android:textSize="15sp"
    android:gravity="center"
    android:layout_toStartOf="@+id/questao"
    android:layout_below="@+id/potencialLetra" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/questao"
        android:layout_alignParentEnd="true"
        android:gravity="center_horizontal"
        android:paddingTop="5dp"
        android:layout_alignStart="@+id/questao">

    <RadioGroup
        android:id="@+id/radioGroupAmc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

    <RadioButton
        android:id="@+id/sim"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sim"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

        <RadioButton
            android:id="@+id/nao"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/questao"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_toEndOf="@+id/sim"
            android:text="Não" />

        <RadioButton
        android:id="@+id/na"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="NA"
        android:layout_below="@+id/questao"
        android:layout_toEndOf="@+id/nao"
        />
        </RadioGroup>
    </LinearLayout>



</RelativeLayout>

Can anyone help me please?

    
asked by anonymous 30.06.2017 / 18:43

2 answers

1

The Merging class must have an attribute and its get /

private int selectedRadioButtonId;
public int getSelectedRadioButtonId(){
    return selectedRadioButtonId;
}
public void setSelectedRadioButtonId(int radioButtonId){
    selectedRadioButtonId = radioButtonId;
}

Change the getView() and onCheckedChanged() methods to read and save the id of the selected RadioButton.

@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);

    //Selecciona o RadioButton
    radioGroupAmc.check(mensalAmc.getSelectedRadioButtonId)

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

            //Guarda o RadioButton seleccionado
            mensalAmc.setSelectedRadioButtonId(checkedId);

            //Julgo que isto já não será necessário.
            /*switch(checkedId) {
                case R.id.sim:    
                        // trata radioValor1
                        break;
                case R.id.nao:
                        // trata radioValor2
                        break;
                case R.id.na:    
                    // trata radioValor3
                    break;
            }*/
        }
    });

    return view;
}
    
03.07.2017 / 20:20
1

After RadioGroup radioGroupAmc ....

Make:

radioGroupAmc.setChecked(mensalAmc.getBooleanDesejado());
    
30.06.2017 / 19:09