【问题标题】:RadioButton Android, selected the same value in other rowRadioButton Android,在其他行中选择了相同的值
【发布时间】:2015-01-01 04:40:35
【问题描述】:

我有一个列表视图,每行有 3 个单选按钮。在测试中,我有 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;
}

【问题讨论】:

  • 你能发布你的活动吗
  • 你在用radiogroup吗?
  • 使用 ViewHolder holder 作为全局并将所有 3 个 RadioButton 放在 RadioGroup 中。
  • 我在 radioGroup 中有单选按钮,但不知道下一步该做什么。

标签: android listview adapter


【解决方案1】:

发生这种情况是因为视图在 ListViews 中被回收以降低内存消耗。因此,它们在 ListView 中的绝对位置发生了变化。所以你可以按照here中提到的这些步骤进行操作

当检查radiobutton时,我们必须调用NotifyDataSetchanged(),以便所有视图都更新。

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

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

对于您的情况,您可以拥有一个与列表大小相同的整数数组,并且数组中的每个值都描述了在第一行中选择的单选按钮的索引(因为您有 3 个单选按钮),依此类推。此外,您必须为每个单选按钮实现 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 中的每一行。希望这可以帮助

【讨论】:

  • 我试过但失败了。你能帮我一些代码吗?
猜你喜欢
  • 1970-01-01
  • 2014-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多