【问题标题】:RadioGroup issue in listview列表视图中的 RadioGroup 问题
【发布时间】:2012-08-10 07:44:30
【问题描述】:

我正在开发琐事应用程序,并在列表视图中显示问题,每个问题都有 4 个单选按钮。这是我的 XML 代码

    <LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"        
    android:id="@+id/radioButtonLayout">
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
 android:visibility="gone">

<RadioButton
    android:id="@+id/radioID_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:saveEnabled="true"
    android:focusableInTouchMode="false"
    android:textColor="#000000" />

<RadioButton
    android:id="@+id/radioID_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:saveEnabled="true"
    android:focusableInTouchMode="false"
    android:textColor="#000000" />

<RadioButton
    android:id="@+id/radioID_3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:saveEnabled="true"
    android:focusableInTouchMode="false"
    android:textColor="#000000" />

<RadioButton
    android:id="@+id/radioID_4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:saveEnabled="true"
    android:focusableInTouchMode="false"
    android:textColor="#000000" />
</RadioGroup>    
</LinearLayout>

这就是我实现单选按钮选择的方式。 getView 方法在这里。

@Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mObjects.size();
    }


    public View getView(final int position, View view, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (view == null) {
            view = inflater.inflate(R.layout.question_option, parent, false);
            holder = new ViewHolder();
            holder.label = (TextView) view.findViewById(R.id.textView1);
            holder.radioButtonLayout = (RadioGroup) view.findViewById(R.id.radioGroup);

            holder.radioButtonFirst = (RadioButton) view.findViewById(R.id.radioID_1);
            holder.radioButtonSecond = (RadioButton) view.findViewById(R.id.radioID_2);
            holder.radioButtonThird = (RadioButton) view.findViewById(R.id.radioID_3);
            holder.radioButtonFourth = (RadioButton) view.findViewById(R.id.radioID_4);

            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }   
        holder.radioButtonFirst.setOnCheckedChangeListener(null);
            holder.radioButtonSecond.setOnCheckedChangeListener(null);
            holder.radioButtonThird.setOnCheckedChangeListener(null);
            holder.radioButtonFourth.setOnCheckedChangeListener(null);

                holder.radioButtonLayout.setVisibility(View.VISIBLE);
                holder.checkboxLayout.setVisibility(View.GONE);
                holder.radioButtonFirst.setChecked(firstItemChecked[position]);
                holder.radioButtonSecond.setChecked(secondItemChecked[position]);
                holder.radioButtonThird.setChecked(thirdItemChecked[position]);
                holder.radioButtonFourth.setChecked(fourthItemChecked[position]);
                holder.radioButtonFirst.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(
                            CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {
                                firstItemChecked[position] = true;

                            } else {
                                firstItemChecked[position] = false;

                            }                               
                        }
                    });
            holder.radioButtonSecond.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(
                        CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        secondItemChecked[position] = true;

                    } else {
                        secondItemChecked[position] = false;
                    }
                }
            });
            holder.radioButtonThird.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(
                        CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                                thirdItemChecked[position] = true;

                            } else {
                                thirdItemChecked[position] = false;

                            }

                        }
                    });
            holder.radioButtonFourth.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(
                        CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                                fourthItemChecked[position] = true;

                            } else {
                                fourthItemChecked[position] = false;

                            }

                        }
                    });
        return view;
    }
  • 现在应用程序有 5 个问题并且 mObjects.size() 在 getCount 方法中是 5。即使 getview 方法被调用超过 5 次。为什么会这样?

  • 选择第一个单选按钮后,我向下和向上滚动列表视图,当我向上滚动查看第一个问题(我选择第一个单选按钮用于回答的问题)时,它失去焦点并显示为未选中。我再次尝试检查第一个选项,这次无法选择第一个单选按钮。一旦我通过单击第二个单选按钮转到第二个选项并尝试,第一个工作正常。

在调试时,我总是将 firstItemchecked(0)(first question first option) 设为 true,即使它是未选中的。

在这个问题上苦苦挣扎超过 4 天。请朋友们帮帮我。 非常感谢。

【问题讨论】:

    标签: android listview radio-group


    【解决方案1】:

    首先尝试使您的代码更易于阅读。例如,我像这样替换了你所有的听众:

    holder.radioButtonFirst.setOnCheckedChangeListener(new CheckChange(position));
    holder.radioButtonSecond.setOnCheckedChangeListener(new CheckChange(position));
    holder.radioButtonThird.setOnCheckedChangeListener(new CheckChange(position));
    holder.radioButtonFourth.setOnCheckedChangeListener(new CheckChange(position));
    ...
    }
    private class CheckChange implements OnCheckedChangeListener {
        private int position;
        private CheckChange(int position) {
            this.position = position;
        }
        @Override
        public void onCheckedChanged(CompoundButton button, boolean isChecked) {
            switch (button.getId()) {
            case R.id.radioID_1:
                firstItemChecked[position] = isChecked;
                break;
            case R.id.radioID_2:
                secondItemChecked[position] = isChecked;
                break;
            case R.id.radioID_3:
                thirdItemChecked[position] = isChecked;
                break;
            case R.id.radioID_4:
                fourthItemChecked[position] = isChecked;
                break;
            default:
                break;
            }
        }
    }
    

    其次,您确定 firstItemChecked[0] 不正确吗?你如何初始化它? 我在 RadioButtons 上使用 setOnChangeListener 时也遇到了一些问题。改为使用 onClickListeners 解决它。

    【讨论】:

    • 感谢您的快速回复。是的,一旦我单击第一个问题的第一个单选按钮,我非常确定 firstItemChecked[0] 为真,这是每个检查的数组。 true 表示已检查。我检查了第一个单选按钮,它变成了真的。我向下滚动以选择它被选中的第三个。当我再次滚动以选择提出的第 4 个问题时,我选择了第一个单选按钮,但我无法选择它,它一直处于未选中状态,我将第 4 个问题的第二个单选按钮设置为已选中,然后我返回到第一个单选按钮第四个问题效果很好。
    • 当我再次向上滚动时 firstItemChecked[0] 未选中,即使值为 true。不知道发生了什么事。这是选择的第 1、3 和 4 个问题单选按钮的数组值。 [真、假、假、假、假] [真、假、真、假、假] [真、假、真、真、假]
    • 在实现 onclicklistener 时如何跟踪 unckecked 状态?
    • 所以...您有 5 个问题...从您的第二条评论中,我看到对于第一个和第三个问题,您至少检查了 2 个按钮...这正常吗?将同一组中的 2 个单选按钮设置为 true?
    • 我不明白是什么导致了问题。当我在回答前两个问题后尝试检查单选按钮时,它没有被选中(我的意思是特定单选按钮的选择不正确)请朋友们帮帮我。
    猜你喜欢
    • 1970-01-01
    • 2016-02-06
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    • 2012-10-18
    • 1970-01-01
    相关资源
    最近更新 更多