RadioGroup在ListView中无法正确保持状态 [英] RadioGroup doesnt keep properly state in ListView

查看:68
本文介绍了RadioGroup在ListView中无法正确保持状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know about ListView recycling issue and try to keep my state in model, but it seems not to work. I've read many topics with the same problem, but their solutions dont work for me, or I do something wrong. When I scroll up and down the list view with radio group in initial state, nothing happens, that's right. But as soon as I set checked any radio button in any row and then scroll it up and down radio buttons become checked randomly.

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>

我希望您能帮助我发现错误,因为我已经完全疲倦了,寻求解决方案.谢谢

I hope you can help me find mistakes, because I am completely tired seeking solution. Thanks

推荐答案

在将 RadioGroup 设置为选中或取消选中之前,应禁用监听器,如下所示.希望这可以帮助.将您的适配器更改为此

Before you are setting checked or unchecked to your RadioGroup, you should disable the listener, like this. Hope this helps. Change your adapter to this

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;
    }

}

这篇关于RadioGroup在ListView中无法正确保持状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆