我有一个列表视图,每行有3个radiobuttons。在测试中我有7行,我正确显示它们,但是当我在一个raddioButton中选择一行时,也在第6行中选择。 不知道为什么会这样。我不明白。
我希望你能帮助我,我留下代码。提前谢谢。
我的适配器
@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;
}
答案 0 :(得分:1)
这种情况正在发生,因为视图在ListViews中被回收以降低内存消耗。因此,它们的绝对位置在ListView中发生了变化。因此,您可以按照here
中提到的步骤进行操作当选中RadioButton时,我们必须调用notifyDataSetChanged(),以便更新所有视图。
当选中RadioButton时,我们必须设置selectedPosition,以跟踪选择哪个RadioButton。
视图在ListViews中被回收。因此,它们的绝对位置在ListView中发生了变化。因此,在getView()内部,我们必须在每个RadioButton上调用setTag()。这允许我们在单击RadioButton时确定列表中RadioButton的当前位置。 RadioButton #setChecked()必须在getView()内部更新,以获取新的或预先存在的视图。
对于您的情况,您可以拥有一个与列表大小相同的整数数组,并且数组中的每个值都描绘了在第一行中选择的radiobutton索引(因为您有3个radiobutton),依此类推。此外,您必须为每个单选按钮实现RadioButton.setOnCheckedChangeListener,并在其中更新相应位置的整数数组的值(例如,如果在行中选择了radiobutton1,则必须在相应位置的整数数组中将索引更新为1) 。并且每次必须检查/取消选中getView()中的每个单选按钮,具体取决于相应位置的整数数组中存在的索引。例如,你的getView()将如下所示
@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;
}
也不要忘记将RadioButtons放在每一行的RadioGroup中。希望这可以帮助