我正在写一个测验程序。它有一个带有两个片段的活动。第一个是回收者对问题的看法。第二个片段包含webview和4个radiobutton,它们存在于radiobutton组中。看起来像这样: Screenshot
当按下单选按钮时,所采取的操作之一是使用回调来提醒回收者视图选择列表中问题项目的复选框,以表明它已被回答。
但是我发现进行回调使得无线电按钮组不会在第一次按下时更新所选的单选按钮。在随后的所有触摸中,radiobutton会相应更新。我已经将我的代码剥离到准系统以找到这个问题,它似乎是回调,但我真的不明白这个问题。我在按钮组上尝试了clearCheck()并手动设置了检查过的radiobutton。两者都没有解决问题。
我包括代码。有问题的OnCreateView片段容器是一个监听器。监听器调用updateDatabaseWithAnswer。 updateDatabaseWithAnswer调用主Activity的回调,然后告诉另一个片段中的recyclerview进行更新。使用代码,recyclelerView可以正确更新,不会抛出任何错误。
解决此问题的唯一方法是注释掉回调的第二行(请参阅回调函数中的注释)。当该行被注释掉时,无线电按钮正常工作。
//QuestionFragement - contains radiobuttons and radiogroup
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
v = inflater.inflate(R.layout.fragment_question, container, false);
//answerButtonGroup
mAnswerButtonGroup =(RadioGroup)v.findViewById(R.id.answerButtonGroup);
mAnswerButtonGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
updateDatabaseWithAnswered();
}
});
return v;
}
private void updateDatabaseWithAnswered(){
int id = mQuestion.getId();
int answerCorrect = 0; //0 = false, 1 = true
if (mQuestion.getUserAnswer().equals(mQuestion.getCorrectAnswer())){
answerCorrect = 1;
}
mQuestion.setTimesUsed(mQuestion.getTimesUsed() + 1);
mQuestion.setTimesCorrect(mQuestion.getTimesCorrect() + answerCorrect);
updateQuestion();
}
private void updateQuestion(){
ExamLab.get(getActivity()).updateDatabaseQuestionAnswered(mQuestion);
//COMMENTING OUT THE FOLLOWING LINE OF CODE FIXES THE RADIOBUTTON PROBLEM
mCallbacks.onQuestionUpdated(mQuestion, 0);
}
//ExamListActivity
/*
// Interface for QuestionFragment.Callback
*/
public void onQuestionUpdated(Question question,int changeSelectedQuestion){
ExamListFragment listFragment = (ExamListFragment) getSupportFragmentManager()
.findFragmentById(R.id.fragment_container);
listFragment.updateUI(changeSelectedQuestion);
}
//ExamListFragment - contains recyclerView
public void updateUI(int changeSelectedQuestion){
ExamLab examLab = ExamLab.get(getActivity());
List<Question> questions = examLab.getQuestions();
if (mAdapter == null){
mAdapter = new QuestionAdapter(questions);
mExamRecyclerView.setAdapter(mAdapter);
/*mAdapter.changeSelectedItem(changeSelectedQuestion);
mAdapter.notifyDataSetChanged();*/
}
else {
mAdapter.changeSelectedItem(changeSelectedQuestion);
mAdapter.notifyDataSetChanged();
}
}
答案 0 :(得分:0)
尝试仅使用onclick功能进行无线电按钮。类似的东西:
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.button1:
if (checked)
//Your code
break;
case R.id.button2:
if (checked)
//Your code
break;
}
}
然后设置onClick =&#34; onRadioButtonClicked&#34;对于每个单选按钮
答案 1 :(得分:0)
在recateView上更新了recyclerView后,操作系统必须再次为QuestionFragment调用onCreateView。通过手动设置已检查的问题在外部 radioButtonGroupListener而不是在中来解决此问题。我仍然不能说我完全理解这里发生了什么,但它现在有效。
//QuestionFragement - contains radiobuttons and radiogroup
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
v = inflater.inflate(R.layout.fragment_question, container, false);
//answerButtonGroup
mAnswerButtonGroup =(RadioGroup)v.findViewById(R.id.answerButtonGroup);
/*NEW CODE HERE*/
if (mQuestion.isUserAnsweredQuestion()) {
setRadioButtonAnsweredDisplay();
setChecked();
}
mAnswerButtonGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
updateDatabaseWithAnswered();
}
});
return v;
}
private void setChecked(){
if (mQuestion.getUserAnswer().equals(mRadioButtonChoice1.getText())){
mRadioButtonChoice1.setChecked(true);
}
else if (mQuestion.getUserAnswer().equals(mRadioButtonChoice2.getText())){
mRadioButtonChoice2.setChecked(true);
}
else if (mQuestion.getUserAnswer().equals(mRadioButtonChoice3.getText())){
mRadioButtonChoice3.setChecked(true);
}
else if (mQuestion.getUserAnswer().equals(mRadioButtonChoice4.getText())){
mRadioButtonChoice4.setChecked(true);
}
}