我如何获得所有答案值?

时间:2017-08-17 12:34:15

标签: java android mysql json android-studio

如何获取所有单选按钮的答案值? 我正在获取单选按钮选择值,但最后一个问题答案值没有通过。如何获得所有问题答案值?我认为当选择最后一个问题时,我的第二个IF条件是正确的。如何获取所有RadioButton值?

   try {
        jsonObject=new JSONObject(json_string);
        jsonArray=jsonObject.getJSONArray("server_response");
        count++;
        if(jsonArray.length()!=0) {
          if(jsonArray.length()==count){
            Toast.makeText(this, "No More Questions", Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(getApplicationContext(),Summary.class);
            ArrayList<String> Result=new ArrayList<String>();
            Result.add(0,String.valueOf(score));
            Result.add(1,String.valueOf(wrong));
            Result.add(2,String.valueOf(unanswer));
            Result.add(3,String.valueOf(correct));
            intent.putExtra("Result",Result);
            intent.putExtra("Questions",(Ques));
            intent.putExtra("urAnswer",urans);
            startActivity(intent);
            finish();
          }


          JSONObject JO = jsonArray.getJSONObject(count);
          Ques = JO.getString("QuestionText");
          Options = JO.getString("Options").split("##");
          ans = Integer.parseInt(JO.getString("CorrectAnswer"));
          RadioButton answer = (RadioButton) findViewById(rgroup.getCheckedRadioButtonId());
          if (answer == null) {
            i = 5;
          } else {
            if (answer.equals(r1)) {
              i = 1;
              Answers.add(r1.getText().toString());
            } else if (answer.equals(r2)) {
              i = 2;
              Answers.add(r1.getText().toString());
            } else if (answer.equals(r3)) {
              i = 3;
              Answers.add(r1.getText().toString());
            } else if (answer.equals(r4)) {
              i = 4;
              Answers.add(r1.getText().toString());
            }
          }
          urans = urans + i;
          if (i == 5) {
            score = score;
            unanswer++;
          } else if (i == ans) {
            score++;
            correct++;
          } else if (!(i == ans)) {
            score = score - 0.25;
            wrong++;
          }
          countDownTimer2.start();
          ques.setText(" Question. No.(" + (QuestionNo + count + 1) + ").\n " + Ques);
          r1.setText(Options[0]);
          r2.setText(Options[1]);
          r3.setText(Options[2]);
          r4.setText(Options[3]);
          rgroup.clearCheck();
        }
      } catch (JSONException e) {
        e.printStackTrace();
      }
    } 

1 个答案:

答案 0 :(得分:0)

count++;
        countDownTimer2.cancel();
        try {
            jsonObject=new JSONObject(json_string);
            jsonArray=jsonObject.getJSONArray("server_response");

            if(jsonArray.length()!=0) {
                RadioButton answer = (RadioButton) findViewById(rgroup.getCheckedRadioButtonId());
                if (answer == null) {
                    i = 5;
                } else {
                    if (answer.equals(r1)) {
                        i = 1;
                        Answers.add(r1.getText().toString());
                    } else if (answer.equals(r2)) {
                        i = 2;
                        Answers.add(r1.getText().toString());
                    } else if (answer.equals(r3)) {
                        i = 3;
                        Answers.add(r1.getText().toString());
                    } else if (answer.equals(r4)) {
                        i = 4;
                        Answers.add(r1.getText().toString());
                    }
                }
                urans = urans + i;
                if (i == 5) {

                    score = score;
                    unanswer++;

                } else if (i == ans) {
                    score++;
                    correct++;

                } else if (!(i == ans)) {

                    score = score - 0.25;
                    wrong++;
                }
                if (jsonArray.length()==count) {
                    Toast.makeText(this, "No More Questions", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(getApplicationContext(), Summary.class);
                    ArrayList<String> Result = new ArrayList<String>();
                    Result.add(0, String.valueOf(score));
                    Result.add(1, String.valueOf(wrong));
                    Result.add(2, String.valueOf(unanswer));
                    Result.add(3, String.valueOf(correct));
                    intent.putExtra("Result", Result);
                    intent.putExtra("Questions", (Ques));
                    intent.putExtra("urAnswer", urans);
                    startActivity(intent);
                    finish();
                }
                JSONObject JO = jsonArray.getJSONObject(count);
                Ques = JO.getString("QuestionText");
                Options = JO.getString("Options").split("##");
                ans = Integer.parseInt(JO.getString("CorrectAnswer"));

                //tv4.setText(String.valueOf("Score:  " + score));
                countDownTimer2.start();
                ques.setText(" Question. No.(" + (QuestionNo + count + 1) + ").\n " + Ques);
                r1.setText(Options[0]);
                r2.setText(Options[1]);
                r3.setText(Options[2]);
                r4.setText(Options[3]);
                rgroup.clearCheck();

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }