Radiogroup in Listview does not maintain selection after scroll

Asked

Viewed 237 times

1

I have a Listview with some information and within it I have a Radiogroup, but when I scroll the screen all my checkbox are unchecked because the listview is creating this checkbox again. I need my checkbox to be marked when scrolling the page.

Follow the code of my Adapter of 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;
        }



    }

Follow the layout code of 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 someone please help me?

2 answers

1


The Evaluator class must have a field and respective get/set methods to store which Radiobutton is sectioned.

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

Change the methods getView() and onCheckedChanged() in order 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;
}
  • I’m trying to implement this way, but what it saves in setSelectedRadioButtonId? is an Int, but I don’t understand this int. Because I need its value to set the radiobutton when scrolling the page.

  • 1

    That int is the id of the selected Radiobutton( R.id.sim, R.id.nao or R.id.na). It is then used in the method radioGroupAmc.check() to select Radiobutton when creating the view in getView().

  • MUCH EASIER! Now it gets simpler to work. Thank you very much! Saving me.

1

After the RadioGroup radioGroupAmc ....

Do:

radioGroupAmc.setChecked(mensalAmc.getBooleanDesejado());
  • I don’t understand. what would getBoolean be().

  • a Boolean of your & #Xa; object that terminates radio status

  • I did not understand , I would like to explain please?

  • @Lucascharles you have to fill the radiobuttons in the ** getView **, when selecting, you mark that selected and in getView you take the value again and arrow in Radiobutton.

Browser other questions tagged

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