Advertisement
chayanforyou

Code of ModeTest.java

May 10th, 2018
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 24.51 KB | None | 0 0
  1. package com.xor.admissiontest.ModelTest;
  2.  
  3. /**
  4.  * Created by shuvro on 3/7/16.
  5.  */
  6.  
  7. import android.annotation.SuppressLint;
  8. import android.content.Intent;
  9. import android.os.Bundle;
  10. import android.os.CountDownTimer;
  11. import android.os.Handler;
  12. import android.support.design.widget.FloatingActionButton;
  13. import android.support.v7.app.AppCompatActivity;
  14. import android.support.v7.widget.AppCompatRadioButton;
  15. import android.text.Html;
  16. import android.util.Log;
  17. import android.view.View;
  18. import android.widget.ProgressBar;
  19. import android.widget.RadioGroup;
  20. import android.widget.TextView;
  21. import android.widget.Toast;
  22.  
  23. import com.google.common.collect.ArrayListMultimap;
  24. import com.google.common.collect.Multimap;
  25. import com.xor.admissiontest.R;
  26. import com.xor.admissiontest.Shared;
  27.  
  28. import org.json.JSONArray;
  29. import org.json.JSONException;
  30. import org.json.JSONObject;
  31. import org.jsoup.Jsoup;
  32. import org.jsoup.nodes.Document;
  33.  
  34. import java.util.ArrayList;
  35. import java.util.Collections;
  36. import java.util.List;
  37. import java.util.Set;
  38. import java.util.concurrent.TimeUnit;
  39.  
  40.  
  41. public class ModelTestActivity extends AppCompatActivity {
  42.     private FloatingActionButton nextButton, previousButton;
  43.     private ProgressBar progressBar;
  44.     private int progressStatus = 0;
  45.     private TextView textView;
  46.     private Handler handler = new Handler();
  47.     //private String urlJsonArry = "http://www.xorbd.com/admission_ninja/tests_questions/model_test_questions/1";
  48.     private String jsonResponse, questionOption, answer, skipquestion;
  49.     private TextView txtResponse, txtResponse1, textviewQuestion, textTime, textviewQuestionNo;
  50.     private AppCompatRadioButton radioButtonOption1, radioButtonOption2, radioButtonOption3, radioButtonOption4;
  51.     private RadioGroup layoutOptions;
  52.     private CounterClass timer;
  53.     private int newQuestionID = 0;
  54.     private int Wrong, Right;
  55.     private Multimap<String, String> quizQuestionModel;
  56.     public int rightAnswerCounter = 0, wrongAnswerCounter = 0;
  57.  
  58.     private ArrayList<Integer> skipList = new ArrayList<Integer>();
  59.     private boolean isSkipped = false;
  60.     private int skippedQuentionID;
  61.  
  62.     private int seconds = 0;
  63.     private long one_second_interval = 1000;
  64.     private long totalExamTimeInMilliseconds = 0;
  65.     private int currentQuestionIDAnswer = 0;
  66.     private int totalQuestionNO = 0;
  67.  
  68.     private int test_id;
  69.  
  70.     @Override
  71.     protected void onCreate(Bundle savedInstanceState) {
  72.         super.onCreate(savedInstanceState);
  73.         getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  74.         setContentView(R.layout.activity_model_test);
  75.  
  76. //        FrameLayout contentFrameLayout = (FrameLayout) findViewById(R.id.content_frame);
  77. //        getLayoutInflater().inflate(R.layout.activity_model_test, contentFrameLayout);
  78. //        txt_toolbar.setText("Model Test");
  79.  
  80.         //get exam duration value from previously activity
  81.         seconds = getIntent().getIntExtra("exam_duration", 0);
  82.         test_id = getIntent().getIntExtra("test_id", 0);
  83.  
  84.         nextButton = (FloatingActionButton) findViewById(R.id.floating_next);
  85.         previousButton = (FloatingActionButton) findViewById(R.id.floating_previous);
  86.         textView = (TextView) findViewById(R.id.textView1);
  87.         textviewQuestion = (TextView) findViewById(R.id.textQuestion);
  88.         textviewQuestionNo = (TextView) findViewById(R.id.textViewQuestionNo);
  89.         textTime = (TextView) findViewById(R.id.textViewTimer);
  90.  
  91.         radioButtonOption1 = (AppCompatRadioButton) findViewById(R.id.option_1);
  92.         radioButtonOption2 = (AppCompatRadioButton) findViewById(R.id.option_2);
  93.         radioButtonOption3 = (AppCompatRadioButton) findViewById(R.id.option_3);
  94.         radioButtonOption4 = (AppCompatRadioButton) findViewById(R.id.option_4);
  95.         layoutOptions = (RadioGroup) findViewById(R.id.LayoutOption);
  96.  
  97.         totalExamTimeInMilliseconds = seconds * 1000;
  98.  
  99.         timer = new CounterClass(totalExamTimeInMilliseconds, one_second_interval);
  100.         timer.start();
  101.  
  102.         getModelTestData();
  103.  
  104.         radioButtonOption1.setOnClickListener(new View.OnClickListener() {
  105.             @Override
  106.             public void onClick(View v) {
  107.                 currentQuestionIDAnswer = 1;
  108.                 Log.d("Selected text::", radioButtonOption1.getText().toString());
  109.                 //Log.d("Question answer::", answerOfQuestion(newQuestionID));
  110.  
  111.                 if (isSkipped) {
  112.                     if (skipList.size() != 0) {
  113.  
  114.                         if (Integer.valueOf(answerOfQuestion(newQuestionID)) == currentQuestionIDAnswer) {
  115.                             //if (radioButtonOption1.getText().toString().contains(answerOfQuestion(skipList.get(skippedQuentionID)))) {
  116.                             rightAnswerCounter++;
  117.                         } else {
  118.                             wrongAnswerCounter++;
  119.                         }
  120.                         skipList.remove(skipList.get(skippedQuentionID));
  121.  
  122.                         if (skipList.size() > 0)
  123.                             requestForSkippedNewQuestion(skipList.get(skippedQuentionID));
  124.  
  125.                         else if (skipList.size() == 0) {
  126.                             showResultPage();
  127.                         }
  128.                     }
  129.                 } else {
  130.                     Log.e("newQuestionID", newQuestionID + "");
  131.  
  132.                     //Log.e("answerttt", answerOfQuestion(newQuestionID));
  133.                     try {
  134.                         if (Integer.valueOf(answerOfQuestion(newQuestionID)) == currentQuestionIDAnswer)
  135.                         //if (radioButtonOption1.getText().toString().contains(answerOfQuestion(newQuestionID)))
  136.                         {
  137.                             rightAnswerCounter++;
  138.                         } else {
  139.                             wrongAnswerCounter++;
  140.                         }
  141.                     } catch (Exception ignored) {
  142.                     }
  143.  
  144.                     if ((newQuestionID + 1) < totalQuestionNO)
  145.                         requestForNewQuestion(newQuestionID + 1);
  146.                     else if ((newQuestionID + 1) == totalQuestionNO) {
  147.                         showResultPage();
  148.                     }
  149.                 }
  150.                 Log.e("isSkipped", String.valueOf(isSkipped));
  151.                 Log.e("right::", String.valueOf(rightAnswerCounter));
  152.                 Log.e("wrong::", String.valueOf(wrongAnswerCounter));
  153.             }
  154.         });
  155.  
  156.         radioButtonOption2.setOnClickListener(new View.OnClickListener() {
  157.             @Override
  158.             public void onClick(View v) {
  159.                 currentQuestionIDAnswer = 2;
  160.                 Log.d("Selected text::", radioButtonOption2.getText().toString());
  161.                 //Log.d("Question answer::", answerOfQuestion(newQuestionID));
  162.  
  163.                 if (isSkipped) {
  164.                     if (skipList.size() != 0) {
  165.                         //Log.d("right::", String.valueOf(answerOfQuestion(skipList.get(skippedQuentionID))));
  166.                         if (Integer.valueOf(answerOfQuestion(newQuestionID)) == currentQuestionIDAnswer) {
  167.                             //if (radioButtonOption2.getText().toString().contains(answerOfQuestion(skipList.get(skippedQuentionID)))) {
  168.                             rightAnswerCounter++;
  169.                         } else {
  170.                             wrongAnswerCounter++;
  171.                         }
  172.  
  173.                         skipList.remove(skipList.get(skippedQuentionID));
  174.  
  175.                         if (skipList.size() > 0)
  176.                             requestForSkippedNewQuestion(skipList.get(skippedQuentionID));
  177.                         else if (skipList.size() == 0) {
  178.                             showResultPage();
  179.                         }
  180.                     }
  181.                 } else {
  182.                     Log.e("newQuestionID", newQuestionID + "");
  183.  
  184.                     try {
  185.                         if (Integer.valueOf(answerOfQuestion(newQuestionID)) == currentQuestionIDAnswer) {
  186.                             rightAnswerCounter++;
  187.                         } else {
  188.                             wrongAnswerCounter++;
  189.                         }
  190.                     } catch (Exception ignored) {
  191.                     }
  192.  
  193.                     if ((newQuestionID + 1) < totalQuestionNO)
  194.                         requestForNewQuestion(newQuestionID + 1);
  195.                     else if ((newQuestionID + 1) == totalQuestionNO) {
  196.                         showResultPage();
  197.                     }
  198.                 }
  199.                 Log.e("isSkipped", String.valueOf(isSkipped));
  200.                 Log.e("right::", String.valueOf(rightAnswerCounter));
  201.                 Log.e("wrong::", String.valueOf(wrongAnswerCounter));
  202.             }
  203.         });
  204.  
  205.         radioButtonOption3.setOnClickListener(new View.OnClickListener() {
  206.             @Override
  207.             public void onClick(View v) {
  208.                 currentQuestionIDAnswer = 3;
  209.                 Log.d("Selected text::", radioButtonOption3.getText().toString());
  210.                 //Log.d("Question answer::", answerOfQuestion(newQuestionID));
  211.  
  212.                 if (isSkipped) {
  213.  
  214.                     if (skipList.size() != 0) {
  215.  
  216.                         //Log.d("right::", String.valueOf(answerOfQuestion(skipList.get(skippedQuentionID))));
  217.                         if (Integer.valueOf(answerOfQuestion(newQuestionID)) == currentQuestionIDAnswer) {
  218.                             //if (radioButtonOption3.getText().toString().contains(answerOfQuestion(skipList.get(skippedQuentionID)))) {
  219.                             rightAnswerCounter++;
  220.                         } else {
  221.                             wrongAnswerCounter++;
  222.                         }
  223.  
  224.                         skipList.remove(skipList.get(skippedQuentionID));
  225.  
  226.                         if (skipList.size() > 0)
  227.                             requestForSkippedNewQuestion(skipList.get(skippedQuentionID));
  228.                         else if (skipList.size() == 0) {
  229.                             showResultPage();
  230.                         }
  231.                     }
  232.                 } else {
  233.                     Log.e("newQuestionID", newQuestionID + "");
  234.  
  235.                     try {
  236.                         if (Integer.valueOf(answerOfQuestion(newQuestionID)) == currentQuestionIDAnswer) {
  237.                             rightAnswerCounter++;
  238.                         } else {
  239.                             wrongAnswerCounter++;
  240.                         }
  241.                     } catch (Exception ignored) {
  242.                     }
  243.  
  244.                     if ((newQuestionID + 1) < totalQuestionNO)
  245.                         requestForNewQuestion(newQuestionID + 1);
  246.                     else if ((newQuestionID + 1) == totalQuestionNO) {
  247.                         showResultPage();
  248.                     }
  249.                 }
  250.                 Log.e("isSkipped", String.valueOf(isSkipped));
  251.                 Log.e("right::", String.valueOf(rightAnswerCounter));
  252.                 Log.e("wrong::", String.valueOf(wrongAnswerCounter));
  253.  
  254.             }
  255.         });
  256.  
  257.         radioButtonOption4.setOnClickListener(new View.OnClickListener() {
  258.             @Override
  259.             public void onClick(View view) {
  260.                 currentQuestionIDAnswer = 4;
  261.                 Log.d("Selected text::", radioButtonOption4.getText().toString());
  262.                 //Log.d("Question answer::", answerOfQuestion(newQuestionID));
  263.                 if (isSkipped) {
  264.  
  265.                     if (skipList.size() != 0) {
  266.  
  267.                         //Log.d("right::", String.valueOf(answerOfQuestion(skipList.get(skippedQuentionID))));
  268.                         if (Integer.valueOf(answerOfQuestion(newQuestionID)) == currentQuestionIDAnswer) {
  269.                             //if (radioButtonOption4.getText().toString().contains(answerOfQuestion(skipList.get(skippedQuentionID)))) {
  270.                             rightAnswerCounter++;
  271.                         } else {
  272.                             wrongAnswerCounter++;
  273.                         }
  274.                         skipList.remove(skipList.get(skippedQuentionID));
  275.  
  276.                         if (skipList.size() > 0)
  277.                             requestForSkippedNewQuestion(skipList.get(skippedQuentionID));
  278.                         else if (skipList.size() == 0) {
  279.                             showResultPage();
  280.                         }
  281.                     }
  282.  
  283.                 } else {
  284.                     Log.e("newQuestionID", newQuestionID + "");
  285.  
  286.                     try {
  287.                         if (Integer.valueOf(answerOfQuestion(newQuestionID)) == currentQuestionIDAnswer) {
  288.                             rightAnswerCounter++;
  289.                         } else {
  290.                             wrongAnswerCounter++;
  291.                         }
  292.                     } catch (Exception ignored) {
  293.                     }
  294.  
  295.                     if ((newQuestionID + 1) < totalQuestionNO)
  296.                         requestForNewQuestion(newQuestionID + 1);
  297.                     else if ((newQuestionID + 1) == totalQuestionNO) {
  298.                         showResultPage();
  299.                     }
  300.                     //printMessage(""+ (newQuestionID + 1));
  301.                 }
  302.                 Log.e("isSkipped", String.valueOf(isSkipped));
  303.                 Log.e("right::", String.valueOf(rightAnswerCounter));
  304.                 Log.e("wrong::", String.valueOf(wrongAnswerCounter));
  305.  
  306.             }
  307.         });
  308.  
  309.         skipButton.setOnClickListener(new View.OnClickListener() {
  310.             @Override
  311.             public void onClick(View view) {
  312.                 if (isSkipped) {
  313.                     skipList.remove(skipList.get(skippedQuentionID));
  314.  
  315.                     if (skipList.size() > 0)
  316.  
  317.                         if (skipList.size() == 0) {
  318.                             showResultPage();
  319.                         }
  320.  
  321.                     requestForSkippedNewQuestion(skipList.get(skippedQuentionID));
  322.  
  323.                 } else {
  324.                     Log.e("newQuestionID", newQuestionID + "");
  325.  
  326.                     skipList.add(newQuestionID);
  327.                     requestForNewQuestion(newQuestionID + 1);
  328.                 }
  329.  
  330.                 Log.e("isSkipped", String.valueOf(isSkipped));
  331.                 Log.e("right::", String.valueOf(rightAnswerCounter));
  332.                 Log.e("wrong::", String.valueOf(wrongAnswerCounter));
  333.  
  334.             }
  335.         });
  336.     }
  337.  
  338.     private void getModelTestData() {
  339.         Log.e("getModelTestData", "enter");
  340.  
  341.         quizQuestionModel = ArrayListMultimap.create();
  342.  
  343.         String testData = Shared.readData("modelTestData", getApplicationContext());
  344.  
  345.         try {
  346.             JSONArray response = new JSONArray(testData);
  347.  
  348.             totalQuestionNO = response.length();
  349.  
  350.             for (int i = 0; i < totalQuestionNO; i++) {
  351.  
  352.                 JSONObject questionModel = (JSONObject) response.get(i);
  353.  
  354.                 String questionID = questionModel.getString("id");
  355.                 String question = questionModel.getString("question");
  356.                 String answer = questionModel.getString("answer");
  357.  
  358.                 Document formattedQuestion = Jsoup.parse(question);
  359.                 //Document formattedAnswer = Jsoup.parse(answer);
  360.  
  361.                 String option1 = questionModel.getString("options1");
  362.                 String option2 = questionModel.getString("options2");
  363.                 //Log.e("option2::", option2);
  364.                 String option3 = questionModel.getString("options3");
  365.                 String option4 = questionModel.getString("options4");
  366.  
  367.                 Document formattedOption1 = Jsoup.parse(option1);
  368.                 Document formattedOption2 = Jsoup.parse(option2);
  369.                 //Log.e("formattedOption2::", formattedOption2.toString());
  370.                 Document formattedOption3 = Jsoup.parse(option3);
  371.                 Document formattedOption4 = Jsoup.parse(option4);
  372.  
  373.                 quizQuestionModel.put(questionID, questionID);
  374.                 quizQuestionModel.put(questionID, formattedQuestion.toString());
  375.                 quizQuestionModel.put(questionID, answer);
  376.                 quizQuestionModel.put(questionID, formattedOption1.toString());
  377.                 quizQuestionModel.put(questionID, formattedOption2.toString());
  378.                 quizQuestionModel.put(questionID, formattedOption3.toString());
  379.                 quizQuestionModel.put(questionID, formattedOption4.toString());
  380.             }
  381.  
  382.             if (!testData.isEmpty()) {
  383.                 requestForNewQuestion(newQuestionID);
  384.             } else {
  385.                 Toast.makeText(getApplicationContext(), "No data loaded!", Toast.LENGTH_LONG).show();
  386.             }
  387.  
  388.         } catch (JSONException e) {
  389.             e.printStackTrace();
  390.         }
  391.     }
  392.  
  393.     public String answerOfQuestion(int questionID) {
  394.  
  395.         String answer;
  396.         Set<String> keys = quizQuestionModel.keySet();
  397.  
  398.         // Putting Set of keys to an arraylist
  399.         List<String> keyStr_list = new ArrayList<>(keys);
  400.  
  401.         //Initializing an Integer arraylist having the same size of its ancestor String arraylist
  402.         List<Integer> keyInt_list = new ArrayList<>(keyStr_list.size());
  403.  
  404.         // Putting values of String arraylist to Integer Arraylist
  405.         for (int i = 0; i < keyStr_list.size(); i++) {
  406.             keyInt_list.add(Integer.parseInt(keyStr_list.get(i)));
  407.         }
  408.         // Sort the Integer arraylist from small to larger values
  409.         Collections.sort(keyInt_list);
  410.  
  411.         //if (keys.contains(String.valueOf(questionID))) {
  412.         Object[] questionObject = quizQuestionModel.get(String.valueOf(keyInt_list.get(questionID))).toArray();
  413.         answer = questionObject[2].toString();
  414.         //}
  415.         Log.e("answer", answer);
  416.         return answer;
  417.     }
  418.  
  419.     @SuppressLint("SetTextI18n")
  420.     public void requestForNewQuestion(int questionID) {
  421.         resetOptionsButton();
  422.  
  423.         // get key(i.e. Question ID) from quizQuestionModel
  424.         Set<String> keys = quizQuestionModel.keySet();
  425.  
  426.         Log.e("question keys", keys.toString());
  427.         // Putting Set of keys to an arraylist
  428.         List<String> keyStr_list = new ArrayList<>(keys);
  429.  
  430.         //Initializing an Integer arraylist having the same size of its ancestor String arraylist
  431.         List<Integer> keyInt_list = new ArrayList<>(keyStr_list.size());
  432.  
  433.         // Putting values of String arraylist to Integer Arraylist
  434.         for (int i = 0; i < keyStr_list.size(); i++) {
  435.             keyInt_list.add(Integer.parseInt(keyStr_list.get(i)));
  436.         }
  437.  
  438.         // Sort the Integer arraylist from small to larger values
  439.         Collections.sort(keyInt_list);
  440.         //Log.e("question id", questionID + "");
  441.         //Log.e("question id value", String.valueOf(keyInt_list.get(questionID)));
  442.  
  443.         if ((newQuestionID + 1) > keys.size()) {
  444.             Log.e("skipped questions id::", String.valueOf(skipList));
  445.  
  446.             if (skipList.size() != 0) {
  447. //                Intent intent = new Intent(MainActivity.this, SkipActivity1.class);
  448. //                intent.putIntegerArrayListExtra("skip_question_id", skipList);
  449. //                intent.putExtra("quizQuestionModel", (Serializable) quizQuestionModel);
  450. //                //intent.putExtra("skip_question_id", skipList);
  451. //                startActivity(intent);
  452. //                finish();
  453.                 isSkipped = true;
  454.  
  455.                 skippedQuentionID = 0;
  456.                 requestForSkippedNewQuestion(skipList.get(skippedQuentionID));
  457.             } else {
  458.                 showResultPage();
  459.             }
  460.         }
  461.  
  462.         //if (keys.contains(String.valueOf(questionID))) {
  463.         Object[] questionObject = quizQuestionModel.get(String.valueOf(keyInt_list.get(questionID))).toArray();
  464.         newQuestionID = questionID; //Integer.valueOf(questionObject[0].toString());
  465.         textviewQuestionNo.setText((newQuestionID + 1) + "/" + totalQuestionNO/*keys.size()*/);
  466.  
  467.         //Log.e("question", questionObject.toString());
  468.         //question
  469.         textviewQuestion.setText(Html.fromHtml(questionObject[1].toString()));
  470.  
  471.         //answer option
  472.         radioButtonOption1.setText(Html.fromHtml(questionObject[3].toString()));
  473.         radioButtonOption2.setText(Html.fromHtml(questionObject[4].toString()));
  474.         radioButtonOption3.setText(Html.fromHtml(questionObject[5].toString()));
  475.         radioButtonOption4.setText(Html.fromHtml(questionObject[6].toString()));
  476.         //}
  477.     }
  478.  
  479.     public void showResultPage() {
  480.         Intent intent = new Intent(ModelTestActivity.this, ResultActivity.class);
  481.         intent.putExtra("test_id", test_id);
  482.         intent.putExtra("rightAnswer", rightAnswerCounter);
  483.         intent.putExtra("wrongAnswer", wrongAnswerCounter);
  484.  
  485.         startActivity(intent);
  486.         finish();
  487.     }
  488.  
  489.     @SuppressLint("SetTextI18n")
  490.     public void requestForSkippedNewQuestion(int questionID) {
  491.  
  492.         resetOptionsButton();
  493.         Set<String> keys = quizQuestionModel.keySet();
  494.         if (keys.contains(String.valueOf(questionID))) {
  495.             Object[] questionObject = quizQuestionModel.get(String.valueOf(questionID)).toArray();
  496.  
  497.             newQuestionID = Integer.valueOf(questionObject[0].toString());
  498.             textviewQuestionNo.setText((newQuestionID + 1) + "/" + keys.size());
  499.  
  500.             //question
  501.             textviewQuestion.setText(Html.fromHtml(questionObject[1].toString()));
  502.  
  503.             //answer option
  504.             radioButtonOption1.setText(Html.fromHtml(questionObject[3].toString()));
  505.             radioButtonOption2.setText(Html.fromHtml(questionObject[4].toString()));
  506.             radioButtonOption3.setText(Html.fromHtml(questionObject[5].toString()));
  507.             radioButtonOption4.setText(Html.fromHtml(questionObject[6].toString()));
  508.         }
  509.     }
  510.  
  511.     private void resetOptionsButton() {
  512.         radioButtonOption1.setChecked(false);
  513.         radioButtonOption2.setChecked(false);
  514.         radioButtonOption3.setChecked(false);
  515.         radioButtonOption4.setChecked(false);
  516.     }
  517.  
  518.     private void getSkipQuestion(int questionID) {
  519.  
  520.         {
  521.             Object[] questionObject = quizQuestionModel.get(String.valueOf(questionID)).toArray();
  522.             newQuestionID = Integer.valueOf(questionObject[0].toString());
  523.  
  524.             //question
  525.             textviewQuestion.setText(Html.fromHtml(questionObject[1].toString()));
  526.  
  527.             //answer option
  528.             radioButtonOption1.setText(Html.fromHtml(questionObject[3].toString()));
  529.             radioButtonOption2.setText(Html.fromHtml(questionObject[4].toString()));
  530.             radioButtonOption3.setText(Html.fromHtml(questionObject[5].toString()));
  531.             radioButtonOption4.setText(Html.fromHtml(questionObject[6].toString()));
  532.         }
  533.  
  534.     }
  535.  
  536.     private boolean isOptionButtonPressed() {
  537.         boolean isPressed = false;
  538.  
  539.         if (radioButtonOption1.isChecked() || radioButtonOption2.isChecked() || radioButtonOption3.isChecked() || radioButtonOption4.isChecked())
  540.             isPressed = true;
  541.  
  542.         return isPressed;
  543.     }
  544.  
  545.     private void printMessage(String message) {
  546.         Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
  547.     }
  548.  
  549.     @Override
  550.     public void onDestroy() {
  551.         timer.cancel();
  552.         super.onDestroy();
  553.     }
  554.  
  555.     public class CounterClass extends CountDownTimer {
  556.         CounterClass(long millisInFuture, long countDownInterval) {
  557.             super(millisInFuture, countDownInterval);
  558.         }
  559.  
  560.         @Override
  561.         public void onFinish() {
  562.             Intent intent = new Intent(ModelTestActivity.this, ResultActivity.class);
  563.             intent.putExtra("test_id", test_id);
  564.             intent.putExtra("rightAnswer", rightAnswerCounter);
  565.             intent.putExtra("wrongAnswer", wrongAnswerCounter);
  566.  
  567.             startActivity(intent);
  568.             finish();
  569.         }
  570.  
  571.         @Override
  572.         public void onTick(long millisUntilFinished) {
  573.             @SuppressLint("DefaultLocale")
  574.             String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millisUntilFinished),
  575.                     TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
  576.                     TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)));
  577.             System.out.println(hms);
  578.             textTime.setText(hms);
  579.         }
  580.     }
  581.  
  582.     @Override
  583.     public boolean onSupportNavigateUp() {
  584.         onBackPressed();
  585.         return true;
  586.     }
  587.  
  588.     @Override
  589.     public void onBackPressed() {
  590.         timer.cancel();
  591.         super.onBackPressed();
  592.     }
  593. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement