如何在MySQL Android测验中获得正确答案

时间:2019-06-15 00:42:18

标签: android mysql json

我正在开发一个android测验,可以使用json从MySQL提取问题,我已经成功地能够从数据库中提取问题,但是由于某种原因,无论我在应用程序中选择的答案如何,总是错误的选项。

我能够使用单选按钮获得正确的答案,但是我想通过按钮实现相同的成就

下面是我的测验活动,如果有人可以告诉我我到底在做错什么,我将不胜感激

更新

仅添加了相关代码

当我尝试验证所选答案时,我认为问题出在oncreate的某处,该问题使用按钮onclick侦听器方法

 if (mButtonChoice1.getText() == mAnswer){
}

测验活动

public class HistoryActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_questions);
        mp = MediaPlayer.create(this, R.raw.beep);
        mTimer = (TextView)findViewById(R.id.timer);
        num = (TextView)findViewById(R.id.textView18);

        main_lay = (LinearLayout)findViewById(R.id.wrong_screen);

        mImageView = (ImageView)findViewById(R.id.imageView2);

        tv1 = (TextView) findViewById(R.id.textView28) ;
//        tv2 = (TextView) findViewById(R.id.textView18) ;
//        tv3 = (TextView) findViewById(R.id.question) ;
        exit = (Button) findViewById(R.id.button4);
        cancel = (ImageButton) findViewById(R.id.cncl);


        param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,1);

        mQuestionView = (TextView)findViewById(R.id.question);
        mButtonChoice1 = (Button)findViewById(R.id.choice1);
        mButtonChoice2 = (Button)findViewById(R.id.choice2);
        mButtonChoice3 = (Button)findViewById(R.id.choice3);
        mButtonChoice4 = (Button)findViewById(R.id.choice4);


//        updateQuestion();


        AsyncJsonObject asyncObject = new AsyncJsonObject();
        asyncObject.execute("");


        //Start of Button Listener for Button1
        mButtonChoice1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(final View view){
                //My logic for Button goes in here

                mAnswer = firstQuestion.getCorrectAnswer();
                String correctAnswerForQuestion = firstQuestion.getCorrectAnswer();
                if (mButtonChoice1.getText() == mAnswer){
                    mScore = mScore + 1;
                    mQuestions+=1;
                    cdn.cancel();
                    mButtonChoice1.startAnimation(animation);
                    mButtonChoice1.setBackgroundColor(Color.parseColor("#86b300"));
                    mButtonChoice1.setClickable(false);
                    mButtonChoice2.setClickable(false);
                    mButtonChoice3.setClickable(false);
                    mButtonChoice4.setClickable(false);
                    if (mQuestionNumber == QuestionLibrary.mQuestions.length) {
                        correctAnswer();

                    } else {
                        final Handler handler = new Handler();
                        handler.postDelayed(new Runnable() {

                            @Override
                            public void run() {
                                view.clearAnimation();
                                currentQuizQuestion++;
                                firstQuestion = parsedObject.get(currentQuizQuestion);
                                mQuestionView.setText(firstQuestion.getQuestion());
                                String option1 = firstQuestion.getOption1();
                                String option2 = firstQuestion.getOption2();
                                String option3 = firstQuestion.getOption3();
                                String option4 = firstQuestion.getOption4();
                                mButtonChoice1.setText(option1);
                                mButtonChoice2.setText(option2);
                                mButtonChoice3.setText(option3);
                                mButtonChoice4.setText(option4);
//                                updateQuestion();
                            }
                        }, 1500);
                    }

                    //This line of code is optional
                    Toast.makeText(HistoryActivity.this, "correct", Toast.LENGTH_SHORT).show();

                }else {


                    if (mButtonChoice2.getText() == correctAnswerForQuestion) {
                        mButtonChoice2.startAnimation(animation);
                        mButtonChoice2.setBackgroundColor(Color.parseColor("#ff9900"));
                    } else if (mButtonChoice3.getText() == correctAnswerForQuestion){
                        mButtonChoice3.startAnimation(animation);
                        mButtonChoice3.setBackgroundColor(Color.parseColor("#ff9900"));
                    } else{
                        mButtonChoice4.startAnimation(animation);
                        mButtonChoice4.setBackgroundColor(Color.parseColor("#ff9900"));
                    }
//                    showNextQuestion();
//                    wrongAnswer();
                    Toast.makeText(HistoryActivity.this, "wrong", Toast.LENGTH_SHORT).show();
                    mButtonChoice1.setBackgroundColor(Color.parseColor("#cc0000"));

                }
                stopSound();


            }

        });

        //End of Button Listener for Button1

        //Start of Button Listener for Button2
        mButtonChoice2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(final View view){
                //My logic for Button goes in here

                if (mButtonChoice2.getText() == mAnswer){
                    mScore = mScore + 1;
                    mQuestions+=1;
                    cdn.cancel();
                    mButtonChoice2.startAnimation(animation);
                    mButtonChoice2.setBackgroundColor(Color.parseColor("#86b300"));
                    mButtonChoice1.setClickable(false);
                    mButtonChoice2.setClickable(false);
                    mButtonChoice3.setClickable(false);
                    mButtonChoice4.setClickable(false);
                    if (mQuestionNumber == QuestionLibrary.mQuestions.length) {
                        correctAnswer();

                    } else {
                        final Handler handler = new Handler();
                        handler.postDelayed(new Runnable() {

                            @Override
                            public void run() {
                                view.clearAnimation();
                                updateQuestion();
                            }
                        }, 1500);
                    }

                    //This line of code is optional
                    Toast.makeText(HistoryActivity.this, "correct", Toast.LENGTH_SHORT).show();

                }else {

                    if (mButtonChoice1.getText() == mAnswer) {
                        mButtonChoice1.startAnimation(animation);
                        mButtonChoice1.setBackgroundColor(Color.parseColor("#ff9900"));
                    } else if (mButtonChoice3.getText() == mAnswer){
                        mButtonChoice3.startAnimation(animation);
                        mButtonChoice3.setBackgroundColor(Color.parseColor("#ff9900"));
                    } else {
                        mButtonChoice4.startAnimation(animation);
                        mButtonChoice4.setBackgroundColor(Color.parseColor("#ff9900"));
                    }
                    wrongAnswer();
                    Toast.makeText(HistoryActivity.this, "wrong", Toast.LENGTH_SHORT).show();
                    mButtonChoice2.setBackgroundColor(Color.parseColor("#cc0000"));
                }
                stopSound();
            }
        });

        //End of Button Listener for Button2


        //Start of Button Listener for Button3
        mButtonChoice3.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(final View view){
                //My logic for Button goes in here

                if (mButtonChoice3.getText() == mAnswer){
                    mScore = mScore + 1;
                    mQuestions+=1;
                    cdn.cancel();
                    mButtonChoice3.startAnimation(animation);
                    mButtonChoice3.setBackgroundColor(Color.parseColor("#86b300"));
                    mButtonChoice1.setClickable(false);
                    mButtonChoice2.setClickable(false);
                    mButtonChoice3.setClickable(false);
                    mButtonChoice4.setClickable(false);
                    if (mQuestionNumber == QuestionLibrary.mQuestions.length) {
                        correctAnswer();

                    } else {
                        final Handler handler = new Handler();
                        handler.postDelayed(new Runnable() {

                            @Override
                            public void run() {
                                view.clearAnimation();
                                updateQuestion();
                            }
                        }, 1500);
                    }

                    //This line of code is optional
                    Toast.makeText(HistoryActivity.this, "correct", Toast.LENGTH_SHORT).show();

                }else {
                    if (mButtonChoice2.getText() == mAnswer) {
                        mButtonChoice2.startAnimation(animation);
                        mButtonChoice2.setBackgroundColor(Color.parseColor("#ff9900"));
                    } else if (mButtonChoice1.getText() == mAnswer){
                        mButtonChoice1.startAnimation(animation);
                        mButtonChoice1.setBackgroundColor(Color.parseColor("#ff9900"));
                    } else {
                        mButtonChoice4.startAnimation(animation);
                        mButtonChoice4.setBackgroundColor(Color.parseColor("#ff9900"));
                    }
                    wrongAnswer();
                    Toast.makeText(HistoryActivity.this, "wrong", Toast.LENGTH_SHORT).show();
                    mButtonChoice3.setBackgroundColor(Color.parseColor("#cc0000"));
                }
                stopSound();
            }
        });

        //End of Button Listener for Button3

        //Start of Button Listener for Button4
        mButtonChoice4.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(final View view){
                //My logic for Button goes in here

                if (mButtonChoice4.getText() == mAnswer){
                    mScore = mScore + 1;
                    mQuestions+=1;
                    cdn.cancel();
                    mButtonChoice4.startAnimation(animation);
                    mButtonChoice4.setBackgroundColor(Color.parseColor("#86b300"));
                    mButtonChoice1.setClickable(false);
                    mButtonChoice2.setClickable(false);
                    mButtonChoice3.setClickable(false);
                    mButtonChoice4.setClickable(false);
                    if (mQuestionNumber == QuestionLibrary.mQuestions.length) {
                        correctAnswer();


                    } else {
                        final Handler handler = new Handler();
                        handler.postDelayed(new Runnable() {

                            @Override
                            public void run() {
                                view.clearAnimation();
                                updateQuestion();
                            }
                        }, 1500);

                    }

                    //This line of code is optional
                    Toast.makeText(HistoryActivity.this, "correct", Toast.LENGTH_SHORT).show();

                }else {

                    if (mButtonChoice2.getText() == mAnswer) {
                        mButtonChoice2.startAnimation(animation);
                        mButtonChoice2.setBackgroundColor(Color.parseColor("#ff9900"));
                    } else if (mButtonChoice3.getText() == mAnswer){
                        mButtonChoice3.startAnimation(animation);
                        mButtonChoice3.setBackgroundColor(Color.parseColor("#ff9900"));
                    } else {
                        mButtonChoice1.startAnimation(animation);
                        mButtonChoice1.setBackgroundColor(Color.parseColor("#ff9900"));
                    }
                    wrongAnswer();
                    Toast.makeText(HistoryActivity.this, "wrong", Toast.LENGTH_SHORT).show();
                    mButtonChoice4.setBackgroundColor(Color.parseColor("#cc0000"));
                }
                stopSound();
            }
        });

    }

    private class AsyncJsonObject extends AsyncTask<String, Void, String> {

        private ProgressDialog progressDialog;

        @Override
        protected String doInBackground(String... params) {

            HttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());
            HttpPost httpPost = new HttpPost("localhost/user/getQuestions.php");
            String jsonResult = "";

            try {
                HttpResponse response = httpClient.execute(httpPost);
                jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
                System.out.println("Returned Json object " + jsonResult.toString());

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return jsonResult;
        }
        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            progressDialog = ProgressDialog.show(HistoryActivity.this, "Downloading Questions","Please Wait....", true);
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            progressDialog.dismiss();
            System.out.println("Resulted Value: " + result);
            parsedObject = returnParsedJsonObject(result);
            if(parsedObject == null){
                return;
            }


            quizCount = parsedObject.size();
            firstQuestion = parsedObject.get(0);
            mQuestionView.setText(firstQuestion.getQuestion());
            mButtonChoice1.setText(firstQuestion.getOption1());
            mButtonChoice2.setText(firstQuestion.getOption2());
            mButtonChoice3.setText(firstQuestion.getOption3());
            mButtonChoice4.setText(firstQuestion.getOption4());
            mAnswer = firstQuestion.getCorrectAnswer();


        }

        private StringBuilder inputStreamToString(InputStream is) {
            String rLine = "";
            StringBuilder answer = new StringBuilder();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));

            try {
                while ((rLine = br.readLine()) != null) {
                    answer.append(rLine);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return answer;
        }
    }
    private List<Question> returnParsedJsonObject(String result){

        List<Question> jsonObject = new ArrayList<Question>();
        JSONObject resultObject = null;
        JSONArray jsonArray = null;
        Question newItemObject = null;

        try {
            resultObject = new JSONObject(result);
            System.out.println("Testing the water " + resultObject.toString());
            jsonArray = resultObject.optJSONArray("questions");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (jsonArray != null) {     // check jsonArray is null?
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonChildNode = null;
                try {
                    jsonChildNode = jsonArray.getJSONObject(i);
                    int id = jsonChildNode.getInt("id");
                    String question = jsonChildNode.getString("question");
                    String answerOption1 = jsonChildNode.getString("option1");
                    String answerOption2 = jsonChildNode.getString("option2");
                    String answerOption3 = jsonChildNode.getString("option3");
                    String answerOption4 = jsonChildNode.getString("option4");
                    String correctAnswer = jsonChildNode.getString("correct_answer");
                    newItemObject = new Question(id, question, answerOption1, answerOption2, answerOption3, answerOption4, correctAnswer);
                    jsonObject.add(newItemObject);
                }
                catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            return jsonObject;
        }
        return jsonObject;
    }

    private void showNextQuestion() {
        currentQuizQuestion++;
        firstQuestion = parsedObject.get(currentQuizQuestion);
        mQuestionView.setText(firstQuestion.getQuestion());
        mButtonChoice1.setText(firstQuestion.getOption1());
        mButtonChoice2.setText(firstQuestion.getOption2());
        mButtonChoice3.setText(firstQuestion.getOption3());
        mButtonChoice4.setText(firstQuestion.getOption4());
        mAnswer = firstQuestion.getCorrectAnswer();
    }

}

0 个答案:

没有答案