【问题标题】:Remember selected state of RadioButton inside a list记住列表中 RadioButton 的选定状态
【发布时间】:2017-03-13 02:45:27
【问题描述】:

我有一个ListView 有几个项目,以便在滚动期间重复使用这些项目。 一个项目由一对TextViews和一个RadioGroup组成,包含3个RadioButtons

我遇到的问题是,当我为 5 个项目中的每一个选择 RadioButton 时,当我向下滚动列表并再次向后滚动时,我一开始所做的选择没有被保存(这意味着没有RadioButton 或错误的会显示为已选中)同时也可能发生某些RadioButtons 将显示为已选中,即使我从未点击过它们。

我正在尝试将所选内容的checkedId 保存在列表中,但没有运气。

按照我的代码:

public View getView(int position, View convertView, ViewGroup parent) {
    ...
    int questionID = question.getQuestionID();
    if (mappedSelectedAnswer.containsKey(questionID)) {
        radioGroup.check(mappedSelectedAnswer.get(questionID));
    } else {
        radioGroup.check(-1);
    }
    ...
}

还有 RadioGroupListener

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId != -1) {
            mappedSelectedAnswer.put(questionID, checkedId);
        }
    ...
    });

mappedSelectedAnswer 是一个带有键 questionID 的 List,值为 checkedId

有人对这个问题有什么建议吗?

谢谢

【问题讨论】:

  • 这可能是由于您的RadioGroup 在您滚动时被回收。 Check out this answer.
  • 正如我已经指定的那样,我已经知道是什么原因,我需要的是一个有效的解决方案。我检查了您链接的答案,但我不明白这一点。你可以说得更详细点吗?谢谢

标签: android radio-button listitem radio-group


【解决方案1】:

您可能需要将它们全部声明到单独的 RadioGroup 中,以便为每个项目选择它们。希望对你有帮助。

谢谢

【讨论】:

  • 你能说得更具体点吗?
【解决方案2】:

你需要三样东西

1- 存储每个 RadioGroup 的选定 ID 的列表

2- RadioGroup 中的一个侦听器,用于在您点击它时设置/重置选定的 id

3- 适配器将在创建视图时检查此列表并选择您保存在侦听器中的项目

【讨论】:

  • 我这样做了,问题是当我滚动并选中单选按钮时,onCheckedListener 也会被调用,并将为另一个项目设置当前值。
  • 监听器看起来不错。您从哪里得到问题和 radioGroup?
  • 问题来自getItem(position),Radiogroup来自convertView.findViewById
【解决方案3】:

例如:

<RadioGroup
    android:id="@+id/group"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content">
    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Standard Taxi (upto 4 passengers)"
        android:id="@+id/standardTaxi"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:onClick="radio"
        android:checked="true" />

    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Wagon(upto 4 passengers)"
        android:id="@+id/wagon"
        android:layout_below="@+id/standardTaxi"
        android:layout_alignParentStart="true"

        android:onClick="radio"
        android:checked="false" />

    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Wheelchair Accessible Taxi"
        android:id="@+id/wheelchair"
        android:layout_below="@+id/wagon"
        android:layout_alignParentStart="true"
        android:onClick="radio"

        android:checked="false" />

    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Van/Maxi(5-11 passengers)"
        android:id="@+id/van"
        android:layout_centerVertical="true"
        android:onClick="radio"
        android:layout_alignParentStart="true"
        android:checked="false" />
</RadioGroup>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 1970-01-01
    相关资源
    最近更新 更多