2014-11-05 78 views
0

我有一个listview,每行有3个单选按钮。在测试中,我有7行,我正确显示它们,但是当我在一个raddioButton中选择一行时,也在第6行中选择。 不知道为什么会发生这种情况。我不明白。RadioButton Android,在其他行中选择相同的值

我希望你能帮助我,我离开代码。提前致谢。

我适配器

@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

你可以发布你的活动 – 2014-11-05 04:22:11

+0

你使用的是radiogroup ?. – 2014-11-05 04:26:11

+0

使用ViewHolder holder作为全局,并将所有3个RadioButton放入RadioGroup。 – 2014-11-05 04:28:07

回答

1

这是因为发生次被回收内部列表视图,以降低内存consumtions。因此,它们的绝对位置在ListView中发生了变化。因此,您可以按照从here

这样的步骤,当检查RadioButton时,我们必须调用notifyDataSetChanged(),以便所有视图都得到更新。

当一个RadioButton被选中时,我们必须设置一个selectedPosition来跟踪哪个RadioButton被选中。

视图在ListViews内循环使用。因此,它们的绝对位置在ListView中发生了变化。因此,在getView()内部,我们必须在每个RadioButton上调用setTag()。这允许我们在单击RadioButton时确定列表中RadioButton的当前位置。 RadioButton#setChecked()必须在新的或预先存在的视图的getView()内更新。

对于您的情况,您可以使用与列表大小相同的整数数组,并且数组中的每个值都描述第一行中选定的单选按钮索引(因为您有3个单选按钮),依此类推。此外,您必须为每个单选按钮实现RadioButton.setOnCheckedChangeListener,并在其内部更新整数数组在相应位置的值(例如,如果在您的行中选择了radiobutton1,则必须将Integer数组中的索引更新为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; 
} 

也不要忘了把你里面单选按钮每RadioGroup中一行。希望这可以帮助

+0

我试过了,失败了。你能帮我一些代码吗? – 2014-11-10 03:28:41