【问题标题】:RadioGroup radio buttons changing state in ArrayAdapter ListViewRadioGroup 单选按钮在 ArrayAdapter ListView 中更改状态
【发布时间】:2018-01-13 12:44:57
【问题描述】:

我强调要找到一个适当的解决方案,以便在滚动问题时保持 listView 组中单选按钮的状态。当我选择一个单选按钮并滚动列表时,单选按钮会随机选择。

public class QuizAdapter extends ArrayAdapter<Questions>{
        
            public QuizAdapter(Context context, ArrayList<Questions> QuestionList){
        
                super(context,0,QuestionList);
            }
        
            @NonNull
            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {
                View QuesView = convertView;
                if(QuesView == null){
                    QuesView = LayoutInflater.from(getContext()).inflate(R.layout.listlayout,parent,false);
                }
                final Questions tempQues = getItem(position);
        
                TextView question = (TextView) QuesView.findViewById(R.id.ques);
                TextView opt1 = (TextView) QuesView.findViewById(R.id.opt1);
                TextView opt2 = (TextView) QuesView.findViewById(R.id.opt2);
                TextView opt3 = (TextView) QuesView.findViewById(R.id.opt3);
                TextView opt4 = (TextView) QuesView.findViewById(R.id.opt4);;
                RadioGroup rg = (RadioGroup) QuesView.findViewById(R.id.rg);
                question.setText(tempQues.getQuestion());
                opt1.setText(tempQues.getOpt1());
                opt2.setText(tempQues.getOpt2());
                opt3.setText(tempQues.getOpt3());
                opt4.setText(tempQues.getOpt4());
        
                rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
        
                        int radioButtonID = group.getCheckedRadioButtonId();
                        RadioButton radioButton = (RadioButton) group.findViewById(radioButtonID);
                        Questions question = getItem(position);
        //for checking current answer and correct answer
                            Log.v("QuizAdapter",radioButton.getText().toString()+" "+question.getAns());
                            notifyDataSetChanged();
                        }
                    });
            
            
                    return QuesView;
                }}

ListLayout.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"
    android:padding="20dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Question?"
        android:id="@+id/ques"
        android:textStyle="bold"
        android:textAppearance="?android:textAppearanceMedium"/>

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingTop="8dp"
        android:id="@+id/rg">


        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="option 1"
            android:id="@+id/opt1" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="option 2"
            android:id="@+id/opt2" />


        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="option 3"
            android:id="@+id/opt3" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="option 4"
            android:id="@+id/opt4" />
    </RadioGroup>

</LinearLayout>

问题类

    public class Questions {
    
        private String question;
        private String opt1;
        private String opt2;
        private String opt3;
        private String opt4;
        private String ans;
    
        public Questions(String question, String opt1, String opt2, String opt3, String opt4, String ans) {
            this.question = question;
            this.opt1 = opt1;
            this.opt2 = opt2;
            this.opt3 = opt3;
            this.opt4 = opt4;
            this.ans = ans;
        }
    
        public String getQuestion() {
            return question;
        }
    
        public String getOpt1() {
            return opt1;
        }
    
        public String getOpt2() {
            return opt2;
        }
    
        public String getOpt3() {
            return opt3;
        }
    
        public String getOpt4() {
            return opt4;
        }
    
        public String getAns() {
            return ans;
        }
        }

计算机测验类

    public class ComputerQuiz extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_computer_quiz);
    
            ArrayList<Questions> computerQuestionsList = new ArrayList<>();
    
            computerQuestionsList.add(new Questions("You should save your computer from","Viruses","Time bombs","Worms", "" +
                    "All of the above","All of the above"));
    
            computerQuestionsList.add(new Questions("A light sensitive device that converts drawing, printed text or other " +
                    "images into digital form is","Keyboard","Scanner","MRE","Mouse","Scanner"));
    
            computerQuestionsList.add(new Questions("The basic architecture of computer was developed by","John Von Neumann",
                    "Charles Babbage","Blaise Pascal","Garden Moore","John Von Neumann"));
    
            computerQuestionsList.add(new Questions("In how many generations a computer can be classified?",
                    "3","4","5","6","5"));
    
            computerQuestionsList.add(new Questions("Fifth generation computers are based on",
                    "Programming Intelligence","System Knowledge","Holograms","Artificial Intelligence","Artificial Intelligence"));
    
            QuizAdapter ComQuestionList = new QuizAdapter(this,computerQuestionsList);
    
           final ListView comList = (ListView) findViewById(R.id.comList);
            comList.setAdapter(ComQuestionList);
    
            View footer = LayoutInflater.from(getBaseContext()).inflate(R.layout.footer_button,null,false);
            comList.addFooterView(footer);
            
        }
    
        }

【问题讨论】:

    标签: android listview radio-button android-arrayadapter radio-group


    【解决方案1】:

    这个问题主要是因为你的Adapter对象重用了。

    所以你应该做的是,你应该以某种方式存储所选项目,并且当它在视图中调用getView 方法时,你应该检查和更新。

    示例:如果您将第二个问题选为 B,而您的屏幕上只有 5 个问题和答案。当您滚动更多时,适配器将尝试重用旧对象,可能是您的第 8 个问题将尝试重用您已回答的第 2 个问题的对象。

    对于您的情况: 当用户选择您可以更新HashMap 的问题时,您可以在adapter 中创建一个HashMap&lt;String, String&gt;。并在getView 内部检查每个问题是否已存在,然后设置单选按钮已选中,其他按钮未选中。

    【讨论】:

    • 感谢您的回复,但您能举个例子吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    相关资源
    最近更新 更多