简体   繁体   中英

RadioButton Android, selected the same value in other row

I have a listview, each row with 3 radiobuttons. In tests I have 7 rows, I display them properly, but when I select one row in one raddioButton, also selected in row 6. Do not know why this happening. I do not understand.

I hope you can help me, I leave the code. Thanks in advance.

my Adapter

@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);
        viewHolder.rdo2 = (RadioButton) rowView.findViewById(R.id.radio1);
        viewHolder.rdo3 = (RadioButton) rowView.findViewById(R.id.radio2);

        rowView.setTag(viewHolder);
    }

    ViewHolder holder = (ViewHolder) rowView.getTag();
    holder.pregunta.setText((position + 1) + ".- " + desc.get(position));
    holder.rdo1.setText(minimo.get(position));
    holder.rdo2.setText(maximo.get(position));
    holder.rdo3.setText("NA");

    return rowView;
}

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

This is happening because Views are recycled inside ListViews to lower memory consumtions. Therefore, their absolute position changes in the ListView. So you can follow these steps as referred from here

when a RadioButton is checked we must call notifyDataSetChanged(), so that all views get updated.

when a RadioButton is checked we must set a selectedPosition, to keep track of which RadioButton is selected.

Views are recycled inside ListViews. Therefore, their absolute position changes in the ListView. Therefore, inside getView(), we must call setTag() on each RadioButton. This allows us to determine the current position of the RadioButton in the list when the RadioButton is clicked. RadioButton#setChecked() must be updated inside getView() for new or pre-existing Views.

For your case,you can have an array of Integers of the same size as that of your list and each value in your array depicts the index of radiobutton(since you have 3 radiobuttons) selected in first row and so on. Also you have to implement RadioButton.setOnCheckedChangeListener for each radiobutton and inside that update the value of your Integer Array at corresponding position (say if radiobutton1 is selected in your row then you have to update the index to 1 in your Integer Array at corresponding position). and every time you have to checked/unchecked the each radiobutton in your getView() depending upon the index present in your Integer Array at corresponding position. For example, your getView() will look like this

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

    if (convertView == null) {
        holder = new ViewHolder();
        LayoutInflater inflater = context.getLayoutInflater();
        convertView = inflater.inflate(R.layout.list_single, null, true);

        holder.pregunta = (TextView) convertView
                .findViewById(R.id.texto_pregunta);
        holder.rdo1 = (RadioButton) convertView.findViewById(R.id.radio0);
        holder.rdo2 = (RadioButton) convertView.findViewById(R.id.radio1);
        holder.rdo3 = (RadioButton) convertView.findViewById(R.id.radio2);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.pregunta.setText((position + 1) + ".- " + desc.get(position));
    holder.rdo1.setText(minimo.get(position));
    holder.rdo2.setText(maximo.get(position));
    holder.rdo3.setText("NA");

    // Intialise yourIntegerArray(same size as that of list) in your constructor with 0 value, and
    //  set index 1 for minimo,2 for maximo and 3 for "NA"
    if (yourIntegerArray[position] == 1) {
        holder.rdo1.setChecked(true);
        holder.rdo2.setChecked(false);
        holder.rdo3.setChecked(false);
    } else if (yourIntegerArray[position] == 2) {
        holder.rdo1.setChecked(false);
        holder.rdo2.setChecked(true);
        holder.rdo3.setChecked(false);
    } else if (yourIntegerArray[position] == 3) {
        holder.rdo1.setChecked(false);
        holder.rdo2.setChecked(false);
        holder.rdo3.setChecked(true);
    } else {
        holder.rdo1.setChecked(false);
        holder.rdo2.setChecked(false);
        holder.rdo3.setChecked(false);
    }

    //don't forget to implement RadioButton.setOnCheckedChangeListener here where you have to update the index inside yourIntegerArray at corresponding position

    return convertView;
}

Also don't forget to put you RadioButtons inside RadioGroup in every row. Hope this can help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM