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?
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.
– Lucas Charles
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 methodradioGroupAmc.check()
to select Radiobutton when creating the view ingetView()
.– ramaral
MUCH EASIER! Now it gets simpler to work. Thank you very much! Saving me.
– Lucas Charles