我已经为array编写了一个代码,但是logcat没有显示错误

z9ju0rcb  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(303)

我面临着一个问题,但它没有显示在logcat,因为它不是一个代码错误或空的问题是,如果问题有2个答案,用户选择他们两个都算在正确的答案,但如果问题只有一个答案,游戏计算它的错误答案,即使用户选择了正确的答案,我会张贴一个屏幕截图,进入图片描述在这里我已经编码正确的答案将是粗体和红色,你可以看到
这是我的问题片段代码

public class QuestionFragment extends Fragment implements IQuestion {

        TextView txt_question_text;
        CheckBox ckbA, ckbB, ckbC, ckbD;
        FrameLayout layout_image;
        ProgressBar progressBar;

        Question question;
        int questionIndex = -1;

        public QuestionFragment() {
            // Required empty public constructor
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            View itemView = inflater.inflate(R.layout.fragment_question, container, false);
            //GET QUESTION
            questionIndex = getArguments().getInt("index", -1);
            question = Common.questionList.get(questionIndex);

            if (question != null) {
                layout_image = (FrameLayout) itemView.findViewById(R.id.layout_image);
                progressBar = (ProgressBar) itemView.findViewById(R.id.progress_bar);
                if (question.isImageQuestion()) {
                    ImageView img_question = (ImageView) itemView.findViewById(R.id.img_question);
                    Picasso.get().load(question.getQuestionImage()).into(img_question, new Callback() {
                        @Override
                        public void onSuccess() {
                            progressBar.setVisibility(View.GONE);
                        }

                        @Override
                        public void onError(Exception e) {
                            Toast.makeText(getContext(), "" + e.getMessage(), Toast.LENGTH_SHORT).show();

                        }
                    });
                } else
                    layout_image.setVisibility(View.GONE);

                //VIEW
                txt_question_text = (TextView) itemView.findViewById(R.id.txt_question_text);
                txt_question_text.setText(question.getQuestionText());

                ckbA = (CheckBox) itemView.findViewById(R.id.ckbA);
                ckbA.setText(question.getAnswerA());
                ckbA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked)
                            Common.selected_values.add(ckbA.getText().toString());
                        else
                            Common.selected_values.remove(ckbA.getText().toString());
                    }
                });

                ckbB = (CheckBox) itemView.findViewById(R.id.ckbB);
                ckbB.setText(question.getAnswerB());
                ckbB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked)
                            Common.selected_values.add(ckbB.getText().toString());
                        else
                            Common.selected_values.remove(ckbB.getText().toString());
                    }
                });

                ckbC = (CheckBox) itemView.findViewById(R.id.ckbC);
                ckbC.setText(question.getAnswerC());
                ckbC.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked)
                            Common.selected_values.add(ckbC.getText().toString());
                        else
                            Common.selected_values.remove(ckbC.getText().toString());
                    }
                });

                ckbD = (CheckBox) itemView.findViewById(R.id.ckbD);
                ckbD.setText(question.getAnswerD());
                ckbD.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked)
                            Common.selected_values.add(ckbD.getText().toString());
                        else
                            Common.selected_values.remove(ckbD.getText().toString());
                    }
                });

            }
            return itemView;
        }

        @Override
        public CurrentQuestion getSelectedAnswer() {
            //THIS METHOD WILL RTURN STATE OF ANSWER
            //RIGHT , WRONG OR NO ANSWER
            CurrentQuestion currentQuestion = new CurrentQuestion(questionIndex, Common.ANSWER_TYPE.NO_ANSWER);//DEFAULT IS NO ANSWER
            StringBuilder result = new StringBuilder();
            if (Common.selected_values.size() > 1) {
                Object[] arrayAnswer = Common.selected_values.toArray();
                for (int i = 0; i < arrayAnswer.length; i++)
                    if (i < arrayAnswer.length - 1)
                        result.append(new StringBuilder(((String) arrayAnswer[i]).substring(0, 1)).append(","));
                    else
                        result.append(new StringBuilder((String) arrayAnswer[i]).substring(0, 1));
            } else if (Common.selected_values.size() == 1) {
                Object[] arrayAnswer = Common.selected_values.toArray();
                result.append((String) arrayAnswer[0]).substring(0, 1);
            }

            if (question != null) {
                //COMPARE CORRECT ANSWER FROM DATABASE WITH USER SELECTED ANSWER
                if (!TextUtils.isEmpty(result)) {
                    if (result.toString().equals(question.getCorrectAnswer()))
                        currentQuestion.setType(Common.ANSWER_TYPE.RIGHT_ANSWER);
                    else
                        currentQuestion.setType(Common.ANSWER_TYPE.WRONG_ANSWER);
                } else
                    currentQuestion.setType(Common.ANSWER_TYPE.NO_ANSWER);
            }
            else {
                Toast.makeText(getContext(), "cannot get question", Toast.LENGTH_SHORT).show();
                currentQuestion.setType(Common.ANSWER_TYPE.NO_ANSWER);
            }
            Common.selected_values.clear();//ALWAYS CLEAR SELECTED ANSWERS AFTER COMPARE IS DONE

            return currentQuestion;
        }

        @Override
        public void showCorrectAnswer() {

            //BOLD CORRECT ANSWER
            //PARTERN:A,B
            String[] correctAnswer = question.getCorrectAnswer().split(",");
            for (String answer : correctAnswer) {
                if (answer.equals("A")) {
                    ckbA.setTypeface(null, Typeface.BOLD);
                    ckbA.setTextColor(Color.RED);
                } else if (answer.equals("B")) {
                    ckbB.setTypeface(null, Typeface.BOLD);
                    ckbB.setTextColor(Color.RED);
                } else if (answer.equals("C")) {
                    ckbC.setTypeface(null, Typeface.BOLD);
                    ckbC.setTextColor(Color.RED);
                } else if (answer.equals("D")) {
                    ckbD.setTypeface(null, Typeface.BOLD);
                    ckbD.setTextColor(Color.RED);
                }
            }

        }

        @Override
        public void disapleAnswer() {

            ckbA.setEnabled(false);
            ckbB.setEnabled(false);
            ckbC.setEnabled(false);
            ckbD.setEnabled(false);

        }

        @Override
        public void resetQuestion() {

            //ENABLE CHECKBOX
            ckbA.setEnabled(true);
            ckbB.setEnabled(true);
            ckbC.setEnabled(true);
            ckbD.setEnabled(true);

            //REMOVE ALL SELECTED
            ckbA.setChecked(false);
            ckbB.setChecked(false);
            ckbC.setChecked(false);
            ckbD.setChecked(false);

            //REMOVE ALL BOLD ON TEXT
            ckbA.setTypeface(null, Typeface.NORMAL);
            ckbA.setTextColor(Color.BLACK);

            ckbB.setTypeface(null, Typeface.NORMAL);
            ckbB.setTextColor(Color.BLACK);

            ckbC.setTypeface(null, Typeface.NORMAL);
            ckbC.setTextColor(Color.BLACK);

            ckbD.setTypeface(null, Typeface.NORMAL);
            ckbD.setTextColor(Color.BLACK);

        }
    }

我认为错误会出现在逻辑问题片段的这一部分,但无法得到它(这是因为正确答案是2个值还是1个值)

@Override
    public CurrentQuestion getSelectedAnswer() {
        //THIS METHOD WILL RTURN STATE OF ANSWER
        //RIGHT , WRONG OR NO ANSWER
        CurrentQuestion currentQuestion = new CurrentQuestion(questionIndex, Common.ANSWER_TYPE.NO_ANSWER);//DEFAULT IS NO ANSWER
        StringBuilder result = new StringBuilder();
        if (Common.selected_values.size() > 1) {
            Object[] arrayAnswer = Common.selected_values.toArray();
            for (int i = 0; i < arrayAnswer.length; i++)
                if (i < arrayAnswer.length - 1)
                    result.append(new StringBuilder(((String) arrayAnswer[i]).substring(0, 1)).append(","));
                else
                    result.append(new StringBuilder((String) arrayAnswer[i]).substring(0, 1));
        } else if (Common.selected_values.size() == 1) {
            Object[] arrayAnswer = Common.selected_values.toArray();
            result.append((String) arrayAnswer[0]).substring(0, 1);
        }
wmtdaxz3

wmtdaxz31#

刚刚找到答案,但我不会删除后,所以如果其他人得到同样的问题
我只是删除了第二部分并编辑了第一部分的条件,如下所示

CurrentQuestion currentQuestion = new CurrentQuestion(questionIndex, Common.ANSWER_TYPE.NO_ANSWER);//DEFAULT IS NO ANSWER
        StringBuilder result = new StringBuilder();
        if (Common.selected_values.size() >= 1) {
            Object[] arrayAnswer = Common.selected_values.toArray();
            for (int i = 0; i < arrayAnswer.length; i++)
                if (i < arrayAnswer.length - 1)
                    result.append(new StringBuilder(((String) arrayAnswer[i]).substring(0, 1)).append(","));
                else
                    result.append(new StringBuilder((String) arrayAnswer[i]).substring(0, 1));
        } /*else if (Common.selected_values.size() == 1) {
            Object[] arrayAnswer = Common.selected_values.toArray();
            result.append((String) arrayAnswer[0]).substring(0, 1);
        }*/

相关问题