ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – 检查“回收视图适配器”中的单选按钮状态,并从MainActivity对其应用更改

2019-07-31 03:24:06  阅读:303  来源: 互联网

标签:android android-recyclerview radio-button radio-group oncheckedchanged


在我的MainActivity方法中,必须修改checkAnswer和showSolution,因为我将RecycleView适配器添加到项目中并将所有视图项移动到QuestionAdapter.我不明白如何在QuestionAdapter中使用isChecked检查rbGroup单选按钮的状态,并将其id传递给MainActivity.
我试着检查一下:Radiogroup in recyclerview
但我仍然不清楚我接下来应该采取什么措施.
有人可以给我基本的指导,从现在起修改我的项目应该采取什么步骤.
仍然无法在回收视图中找到处理单选按钮列表的答案或教程.如何使用MainActivity界面检查单选按钮状态?

根据建议进行了一些更新.不明白我应该如何修改showSolution方法.

public class MainActivity extends AppCompatActivity {

public QuestionAdapter adapter;
public ArrayList<Question> questionList;
private int questionCountTotal;
private long backPressedTime;
private int score;
private int questionCounter;
private Button btnConfirmNext;
private boolean answered;
private Question currentQuestion;
private TextView tvQuestion, tvScore, tvQuestionCount, tvCountdown;
public QuizDbHelper dbHelper;
RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnConfirmNext = findViewById(R.id.confirm_button);
    tvCountdown = findViewById(R.id.count_down);
    tvScore = findViewById(R.id.text_view_score);

    recyclerView = findViewById(R.id.recycler_view);
    recyclerView.setNestedScrollingEnabled(false);
    recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL));
    questionList = new ArrayList<>();
    adapter = new QuestionAdapter(this, questionList);

    RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 1);
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(adapter);

    dbHelper = new QuizDbHelper(this);
    questionList = dbHelper.getAllQuestions();
    questionCountTotal = questionList.size();
    Collections.shuffle(questionList);

        prepareQuestion();

    btnConfirmNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            checkAnswer();
        }
    });

}


private void prepareQuestion() {
    adapter = new QuestionAdapter(getApplicationContext(), questionList);
    recyclerView.setAdapter(adapter);
    if (questionCounter < questionCountTotal) {
        currentQuestion = questionList.get(questionCounter);

        answered = false;
        btnConfirmNext.setText("Confirm");

    } else {
        finishQuiz();
    }
}


//How should I handle it in onbindViewHolder

private void checkAnswer() {
    answered = true;
    countDownTimer.cancel();
    RadioButton rbSelected = findViewById(rbGroup.getCheckedRadioButtonId());
    int answerNb = rbGroup.indexOfChild(rbSelected) + 1;

    if (answerNb == currentQuestion.getAnswerNB()) {
        score++;
        tvScore.setText("Score: " + score);
    }
    showSolution();
}

//How should I change state of the items in recycle view

private void showSolution() {
    rb1.setTextColor(Color.RED);
    rb2.setTextColor(Color.RED);
    rb3.setTextColor(Color.RED);
    rb4.setTextColor(Color.RED);

    switch (currentQuestion.getAnswerNB()) {
        case 1:
            rb1.setTextColor(Color.GREEN);
            break;
        case 2:
            rb2.setTextColor(Color.GREEN);
            break;
        case 3:
            rb3.setTextColor(Color.GREEN);
            break;
        case 4:
            rb4.setTextColor(Color.GREEN);
            break;
    }

    btnConfirmNext.setText("Finish");

}

QuestionAdapter

public class QuestionAdapter extends RecyclerView.Adapter<QuestionAdapter.MyViewHolder> {

public ArrayList<Question> questionList;

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView tvQuestion, tvScore, tvQuestionCount, tvCountdown;
    public RadioGroup rbGroup;
    public RadioButton rb1, rb2, rb3, rb4;



    public MyViewHolder(View view) {
        super(view);
        tvQuestion = view.findViewById(R.id.question);
        rbGroup = view.findViewById(R.id.radiog_group);
        rb1 = view.findViewById(R.id.radio_button1);
        rb2 = view.findViewById(R.id.radio_button2);
        rb3 = view.findViewById(R.id.radio_button3);
        rb4 = view.findViewById(R.id.radio_button4);
    }
}


public QuestionAdapter(Context mContext, ArrayList<Question> questionList) {
    this.questionList = questionList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.question_card, parent, false);


    return new MyViewHolder(itemView);
}

@NonNull
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
    final Question question = questionList.get(position);
    holder.tvQuestion.setText(question.getQuestion());
    holder.rb1.setText(question.getOption1());
    holder.rb2.setText(question.getOption2());
    holder.rb3.setText(question.getOption3());
    holder.rb4.setText(question.getOption4());

    holder.rbGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {

// How can I handle Check status here and pass it to main activity?

        }
    });
}

@Override
public int getItemCount() {
    return questionList.size();
}
}

UPDATE.
 在我的QuestionAdapter中添加接口并对构造函数和其他部分进行更改后,我在MainActivity中的checkAnswer看起来像这样

    private void checkAnswer() {
    answered = true;
    countDownTimer.cancel();
    adapter = new QuestionAdapter(getApplicationContext(), questionList, new QuestionAdapter.OnItemListener() {
        @Override
        public void onItemSelect(int position) {
           if (position+1==currentQuestion.getAnswerNB()){
               score++;
               tvScore.setText("Score: " + score);
           }
        }
        });
    showSolution();
}

现在我应该用showSolution方法做什么?
似乎在checkAnswer之后我应该将信息发送回QuestionAdapter并在那里进行setTextColor.可能我走错了路……

MainActivity

解决方法:

你可以通过使用界面来做到这一点.

创建一个这样的接口:

public interface OnItemListener {
    public void onItemSelect(int position);
}

然后在你的适配器中更改构造函数,如下所示:

  QuestionAdapter(Context mContext, ArrayList<Question> questionList, OnItemListener 
  onItemListener) {
        this.onItemListener = onItemListener;
        this.questionList = questionList;
    }

并在onCheckedChangeListener中添加此行

            onItemListener.onItemSelect(position);

像这样:

holder.rbGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {

        // How can I handle Check status here and pass it to main activity?
        onItemListener.onItemSelect(position);
        }
    });

然后在你的活动中添加你定义适配器的地方(它定义适配器的另一种方式,你也可以用自己的方式记住添加这样的接口(new OnItemListener …)):

 dbHelper = new QuizDbHelper(getApplicationContext(), questionList, new 
 OnItemListener({

            @Override
            public void onItemSelect(int position) {
             // handle your check state of adapter item here
            }

        });

标签:android,android-recyclerview,radio-button,radio-group,oncheckedchanged
来源: https://codeday.me/bug/20190731/1586509.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有