【问题标题】:RadioGroup doesnt keep properly state in ListViewRadioGroup 在 ListView 中没有保持正确的状态
【发布时间】:2016-06-21 03:01:09
【问题描述】:

我知道 ListView 回收问题并尝试将我的状态保持在模型中,但它似乎不起作用。我已经阅读了许多具有相同问题的主题,但他们的解决方案对我不起作用,或者我做错了什么。 当我在初始状态下使用单选组向上和向下滚动列表视图时,没有任何反应,没错。但是一旦我设置了选中任何行中的任何单选按钮,然后上下滚动单选按钮就会随机选中。

RadioAdapter.java:

public class RadioAdapter extends ArrayAdapter<Question> {

List<Question> mSource;

static class ViewHolder {
    TextView category = null;
    TextView question = null;
    RadioGroup rbGroup = null;

    ViewHolder(View row) {
        this.category = (TextView) row.findViewById(R.id.tvQuestionCategory);
        this.question = (TextView) row.findViewById(R.id.tvQuestion);
        this.rbGroup = (RadioGroup) row.findViewById(R.id.rgIteration);
    }
}

private LayoutInflater mInflater;

public RadioAdapter(Context context, List<Question> mSource) {
    super(context, R.layout.item_question, mSource);
    mInflater = LayoutInflater.from(context);
    this.mSource = mSource;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    View row = convertView;

    if (row == null) {
        row = mInflater.inflate(R.layout.item_question, null);

        holder = new ViewHolder(row);

        holder.rbGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            public void onCheckedChanged(RadioGroup group, int checkedId) {
                //Get position of current group
                Integer pos = (Integer) group.getTag();
                switch (checkedId) {
                    case R.id.rbIterationYes:
                        //Check 'YES' and uncheck 'NO'
                        mSource.get(pos).setYesChecked(true);
                        mSource.get(pos).setNoChecked(false);
                        break;

                    case R.id.rbIterationNo:
                        //Vice versa
                        mSource.get(pos).setNoChecked(true);
                        mSource.get(pos).setYesChecked(false);
                        break;
                }
            }
        });

        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();
    }
    holder.rbGroup.setTag(new Integer(position));
    holder.category.setText(mSource.get(position).getCategory());
    holder.question.setText(mSource.get(position).getDescription());
    //If no one of buttons isn't checked
    if (!mSource.get(position).isYesChecked() && !mSource.get(position).isNoChecked()) {
        holder.rbGroup.clearCheck();
    }else {
        //We are supposing 'YES' is checked
        int c = 0;
        //Or 'NO' is checked
        if (mSource.get(position).isNoChecked()){
            c = 1;
        }
        //Set checked button
        ((RadioButton)holder.rbGroup.getChildAt(c)).setChecked(true);
    }
    return row;
}

}

Question.java:

public class Question {

     private Integer id;
     private String category;
     private String description;
     private boolean yesChecked;
     private boolean noChecked;

/* Getters and setters */
}

item_question.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="18dp"
    android:id="@+id/tvQuestionCategory"/>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="18dp"
    android:id="@+id/tvQuestion"/>

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/rgIteration">
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"
            android:id="@+id/rbIterationYes"
            android:text="@string/rbIterationYes"
            android:textSize="16dp"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"
            android:id="@+id/rbIterationNo"
            android:text="@string/rbIterationNo"
            android:textSize="16dp" />
</RadioGroup>

</LinearLayout>

我希望你能帮我找出错误,因为我完全厌倦了寻求解决方案。谢谢

【问题讨论】:

  • 调试您的代码并检查您在 Integer pos = (Integer) group.getTag(); 中的正确位置和 holder.rbGroup.setTag(new Integer(position));在这些代码行上。
  • 调试显示正确的位置,但是当我向上和向下滚动时有时会起作用 public void onCheckedChanged(RadioGroup group, int checkedId)
  • 我已经用复选框解决了这个问题,检查一下,希望它也适用于广播组。 androprogrammer.com/2013/10/…
  • 还是不行
  • 您已经在存储问题 ID,那么为什么需要位置。只需使用 id 作为标识符,然后检查是否检查了是或否。

标签: android listview radio-group


【解决方案1】:

在您为RadioGroup 设置选中或取消选中之前,您应该像这样禁用监听器。希望这可以帮助。 把你的适配器改成这个

public class RadioAdapter extends ArrayAdapter<Question> {

    List<Question> mSource;
    RadioGroup.OnCheckedChangeListener listener;
    static class ViewHolder {
        TextView category = null;
        TextView question = null;
        RadioGroup rbGroup = null;

        ViewHolder(View row) {
            this.category = (TextView) row.findViewById(R.id.tvQuestionCategory);
            this.question = (TextView) row.findViewById(R.id.tvQuestion);
            this.rbGroup = (RadioGroup) row.findViewById(R.id.rgIteration);
        }
    }

    private LayoutInflater mInflater;

    public RadioAdapter(Context context, List<Question> mSource) {
        super(context, R.layout.item_question, mSource);
        mInflater = LayoutInflater.from(context);
        this.mSource = mSource;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        View row = convertView;

        if (row == null) {
            row = mInflater.inflate(R.layout.item_question, null);

            holder = new ViewHolder(row);
            listener = new RadioGroup.OnCheckedChangeListener() {

                public void onCheckedChanged(RadioGroup group, int checkedId) {

                    //Get position of current group
                    Integer pos = (Integer) group.getTag();
                    switch (checkedId) {
                        case R.id.rbIterationYes:
                            //Check 'YES' and uncheck 'NO'
                            mSource.get(pos).setYesChecked(true);
                            mSource.get(pos).setNoChecked(false);
                            break;

                        case R.id.rbIterationNo:
                            //Vice versa
                            mSource.get(pos).setNoChecked(true);
                            mSource.get(pos).setYesChecked(false);
                            break;
                    }
                }
            };
            holder.rbGroup.setOnCheckedChangeListener(listener);

            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }
        holder.rbGroup.setTag(new Integer(position));
        holder.category.setText(mSource.get(position).getCategory());
        holder.question.setText(mSource.get(position).getDescription());
        //If no one of buttons isn't checked
        holder.rbGroup.setOnCheckedChangeListener(null);
        if (!mSource.get(position).isYesChecked() && !mSource.get(position).isNoChecked()) {
            holder.rbGroup.clearCheck();
        }else {
            //We are supposing 'YES' is checked
            int c = 0;
            //Or 'NO' is checked
            if (mSource.get(position).isNoChecked()){
                c = 1;
            }
            //Set checked button

            ((RadioButton)holder.rbGroup.getChildAt(c)).setChecked(true);

        }
        holder.rbGroup.setOnCheckedChangeListener(listener);
        return row;
    }

}

【讨论】:

    【解决方案2】:

    您可以像这样覆盖 getItemId 方法并返回您的问题 id,这样您就可以使用 id 识别每个问题和答案。

    public class RadioAdapter extends ArrayAdapter<Question> {
    
        List<Question> mSource;
    
        static class ViewHolder {
            TextView category = null;
            TextView question = null;
            RadioGroup rbGroup = null;
    
            ViewHolder(View row) {
                this.category = (TextView) row.findViewById(R.id.tvQuestionCategory);
                this.question = (TextView) row.findViewById(R.id.tvQuestion);
                this.rbGroup = (RadioGroup) row.findViewById(R.id.rgIteration);
            }
        }
    
        private LayoutInflater mInflater;
    
        public RadioAdapter(Context context, List<Question> mSource) {
            super(context, R.layout.item_question, mSource);
            mInflater = LayoutInflater.from(context);
            this.mSource = mSource;
        }
    
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            View row = convertView;
    
            if (row == null) {
                row = mInflater.inflate(R.layout.item_question, null);
    
                holder = new ViewHolder(row);
    
                holder.rbGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        //Get position of current group
                        //Integer pos = (Integer) group.getTag();
                        switch (checkedId) {
                            case R.id.rbIterationYes:
                                //Check 'YES' and uncheck 'NO'
                                mSource.get((int) getItemId(position)).setYesChecked(true);
                                mSource.get((int) getItemId(position)).setNoChecked(false);
                                break;
    
                            case R.id.rbIterationNo:
                                //Vice versa
                                mSource.get((int) getItemId(position)).setNoChecked(true);
                                mSource.get((int) getItemId(position)).setYesChecked(false);
                                break;
                        }
                    }
                });
    
                row.setTag(holder);
            } else {
                holder = (ViewHolder) row.getTag();
            }
            holder.rbGroup.setTag(new Integer(position));
            holder.category.setText(mSource.get(position).getCategory());
            holder.question.setText(mSource.get(position).getDescription());
            //If no one of buttons isn't checked
            if (!mSource.get((int) getItemId(position)).isYesChecked() && !mSource.get((int) getItemId(position)).isNoChecked()) {
                holder.rbGroup.clearCheck();
            } else {
                //We are supposing 'YES' is checked
                int c = 0;
                //Or 'NO' is checked
                if (mSource.get((int) getItemId(position)).isNoChecked()) {
                    c = 1;
                }
                //Set checked button
                ((RadioButton) holder.rbGroup.getChildAt(c)).setChecked(true);
            }
            return row;
        }
    
        @Override
        public long getItemId(int position) {
            return mSource.get(position).getId();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-17
      • 2013-06-12
      • 2018-01-13
      • 2018-10-22
      相关资源
      最近更新 更多