Novus Ordo Seclorum
Novus Ordo Seclorum

Reputation: 123

Custom Listview with Radiobutton, selected several .. Android

I have a custom Listview like the following image. But to select the first radiobutton, also selects the one after the scroll ends, according eh read that is by recycling views using android. I tried several solutions but I have not been successful .. If anyone has been the same would like to see if I can help .. Here is the code for my Apadapter you take a look .. Cheers ... !!

Link Image > http://sia1.subirimagenes.net/img/2014/11/10/141110041729113463.jpg

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View rowView = convertView;

    if (rowView == null) {

        LayoutInflater inflater = context.getLayoutInflater();
        rowView = inflater.inflate(R.layout.list_single, null, true);
        ViewHolder viewHolder = new ViewHolder();

        viewHolder.pregunta = (TextView) rowView.findViewById(R.id.texto_pregunta);
        viewHolder.rdo1 = (RadioButton) rowView.findViewById(R.id.radio0);

        rowView.setTag(viewHolder);
    }

    ViewHolder holder = (ViewHolder) rowView.getTag();
    holder.pregunta.setText((position + 1) + ".- " + desc.get(position));
    holder.rdo1.setText(minimo.get(position));
    return rowView;
}

public static class ViewHolder {
    public TextView pregunta;
    public RadioButton rdo1;
    public RadioButton rdo2;
    public RadioButton rdo3;
}

Upvotes: 1

Views: 750

Answers (2)

Novus Ordo Seclorum
Novus Ordo Seclorum

Reputation: 123

Thanks to those who responded. reading the responses I received, I found the solution.

I leave my adapter to use it if they have the same problem as me ...

public class CustomList extends ArrayAdapter<String> {

private final Activity context;
private final ArrayList<String> minimo;
private final ArrayList<String> maximo;

private ArrayList<Boolean> status = new ArrayList<Boolean>();

public CustomList(Activity context, ArrayList<String> min, ArrayList<String> max) {
    super(context, R.layout.list_single, min);
    this.context = context;
    this.minimo = min;
    this.maximo = max;

    //initialize all with false
    for (int i = 0; i < minimo.size(); i++) {
        status.add(false);
    }
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View rowView = convertView;

    LayoutInflater inflater = context.getLayoutInflater();
    rowView = inflater.inflate(R.layout.list_single, null, true);
    ViewHolder viewHolder = new ViewHolder();

    viewHolder.pregunta = (TextView) rowView.findViewById(R.id.texto_pregunta);
    viewHolder.rdo1 = (RadioButton) rowView.findViewById(R.id.radio0);
    rowView.setTag(viewHolder);

    ViewHolder holder = (ViewHolder) rowView.getTag();
    holder.pregunta.setText("Some Text Answer");
    holder.rdo1.setText(minimo.get(position));
    holder.rdo1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            //change status when clicked
            status.set(position, isChecked);

            //verified status all radiobuttons
            for (int i = 0; i < status.size(); i++) {
                Log.v("Log", "" + status.get(i));
            }
        }
    });
    holder.rdo1.setChecked(status.get(position));
    return rowView;
}

public static class ViewHolder {
    public TextView pregunta;
    public RadioButton rdo1;
    public RadioButton rdo2;
    public RadioButton rdo3;
}

}

Upvotes: 1

romtsn
romtsn

Reputation: 12002

You can store the checked value in your model. I see that you use lists to store some data, so you can also create one more list with checked, and then use it in getView method:

private ArrayList<Boolean> checked;

holder.rdo1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            checked.set(position, isChecked);
        }
});
holder.rdo1.setChecked(checked.get(position));

Upvotes: 0

Related Questions