Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.xor.admissiontest.ModelTest;
- /**
- * Created by shuvro on 3/7/16.
- */
- import android.annotation.SuppressLint;
- import android.content.Intent;
- import android.os.Bundle;
- import android.os.CountDownTimer;
- import android.os.Handler;
- import android.support.design.widget.FloatingActionButton;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.AppCompatRadioButton;
- import android.text.Html;
- import android.util.Log;
- import android.view.View;
- import android.widget.ProgressBar;
- import android.widget.RadioGroup;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.google.common.collect.ArrayListMultimap;
- import com.google.common.collect.Multimap;
- import com.xor.admissiontest.R;
- import com.xor.admissiontest.Shared;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import org.jsoup.Jsoup;
- import org.jsoup.nodes.Document;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- import java.util.Set;
- import java.util.concurrent.TimeUnit;
- public class ModelTestActivity extends AppCompatActivity {
- private FloatingActionButton nextButton, previousButton;
- private ProgressBar progressBar;
- private int progressStatus = 0;
- private TextView textView;
- private Handler handler = new Handler();
- //private String urlJsonArry = "http://www.xorbd.com/admission_ninja/tests_questions/model_test_questions/1";
- private String jsonResponse, questionOption, answer, skipquestion;
- private TextView txtResponse, txtResponse1, textviewQuestion, textTime, textviewQuestionNo;
- private AppCompatRadioButton radioButtonOption1, radioButtonOption2, radioButtonOption3, radioButtonOption4;
- private RadioGroup layoutOptions;
- private CounterClass timer;
- private int newQuestionID = 0;
- private int Wrong, Right;
- private Multimap<String, String> quizQuestionModel;
- public int rightAnswerCounter = 0, wrongAnswerCounter = 0;
- private ArrayList<Integer> skipList = new ArrayList<Integer>();
- private boolean isSkipped = false;
- private int skippedQuentionID;
- private int seconds = 0;
- private long one_second_interval = 1000;
- private long totalExamTimeInMilliseconds = 0;
- private int currentQuestionIDAnswer = 0;
- private int totalQuestionNO = 0;
- private int test_id;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- getSupportActionBar().setDisplayHomeAsUpEnabled(true);
- setContentView(R.layout.activity_model_test);
- // FrameLayout contentFrameLayout = (FrameLayout) findViewById(R.id.content_frame);
- // getLayoutInflater().inflate(R.layout.activity_model_test, contentFrameLayout);
- // txt_toolbar.setText("Model Test");
- //get exam duration value from previously activity
- seconds = getIntent().getIntExtra("exam_duration", 0);
- test_id = getIntent().getIntExtra("test_id", 0);
- nextButton = (FloatingActionButton) findViewById(R.id.floating_next);
- previousButton = (FloatingActionButton) findViewById(R.id.floating_previous);
- textView = (TextView) findViewById(R.id.textView1);
- textviewQuestion = (TextView) findViewById(R.id.textQuestion);
- textviewQuestionNo = (TextView) findViewById(R.id.textViewQuestionNo);
- textTime = (TextView) findViewById(R.id.textViewTimer);
- radioButtonOption1 = (AppCompatRadioButton) findViewById(R.id.option_1);
- radioButtonOption2 = (AppCompatRadioButton) findViewById(R.id.option_2);
- radioButtonOption3 = (AppCompatRadioButton) findViewById(R.id.option_3);
- radioButtonOption4 = (AppCompatRadioButton) findViewById(R.id.option_4);
- layoutOptions = (RadioGroup) findViewById(R.id.LayoutOption);
- totalExamTimeInMilliseconds = seconds * 1000;
- timer = new CounterClass(totalExamTimeInMilliseconds, one_second_interval);
- timer.start();
- getModelTestData();
- radioButtonOption1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- currentQuestionIDAnswer = 1;
- Log.d("Selected text::", radioButtonOption1.getText().toString());
- //Log.d("Question answer::", answerOfQuestion(newQuestionID));
- if (isSkipped) {
- if (skipList.size() != 0) {
- if (Integer.valueOf(answerOfQuestion(newQuestionID)) == currentQuestionIDAnswer) {
- //if (radioButtonOption1.getText().toString().contains(answerOfQuestion(skipList.get(skippedQuentionID)))) {
- rightAnswerCounter++;
- } else {
- wrongAnswerCounter++;
- }
- skipList.remove(skipList.get(skippedQuentionID));
- if (skipList.size() > 0)
- requestForSkippedNewQuestion(skipList.get(skippedQuentionID));
- else if (skipList.size() == 0) {
- showResultPage();
- }
- }
- } else {
- Log.e("newQuestionID", newQuestionID + "");
- //Log.e("answerttt", answerOfQuestion(newQuestionID));
- try {
- if (Integer.valueOf(answerOfQuestion(newQuestionID)) == currentQuestionIDAnswer)
- //if (radioButtonOption1.getText().toString().contains(answerOfQuestion(newQuestionID)))
- {
- rightAnswerCounter++;
- } else {
- wrongAnswerCounter++;
- }
- } catch (Exception ignored) {
- }
- if ((newQuestionID + 1) < totalQuestionNO)
- requestForNewQuestion(newQuestionID + 1);
- else if ((newQuestionID + 1) == totalQuestionNO) {
- showResultPage();
- }
- }
- Log.e("isSkipped", String.valueOf(isSkipped));
- Log.e("right::", String.valueOf(rightAnswerCounter));
- Log.e("wrong::", String.valueOf(wrongAnswerCounter));
- }
- });
- radioButtonOption2.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- currentQuestionIDAnswer = 2;
- Log.d("Selected text::", radioButtonOption2.getText().toString());
- //Log.d("Question answer::", answerOfQuestion(newQuestionID));
- if (isSkipped) {
- if (skipList.size() != 0) {
- //Log.d("right::", String.valueOf(answerOfQuestion(skipList.get(skippedQuentionID))));
- if (Integer.valueOf(answerOfQuestion(newQuestionID)) == currentQuestionIDAnswer) {
- //if (radioButtonOption2.getText().toString().contains(answerOfQuestion(skipList.get(skippedQuentionID)))) {
- rightAnswerCounter++;
- } else {
- wrongAnswerCounter++;
- }
- skipList.remove(skipList.get(skippedQuentionID));
- if (skipList.size() > 0)
- requestForSkippedNewQuestion(skipList.get(skippedQuentionID));
- else if (skipList.size() == 0) {
- showResultPage();
- }
- }
- } else {
- Log.e("newQuestionID", newQuestionID + "");
- try {
- if (Integer.valueOf(answerOfQuestion(newQuestionID)) == currentQuestionIDAnswer) {
- rightAnswerCounter++;
- } else {
- wrongAnswerCounter++;
- }
- } catch (Exception ignored) {
- }
- if ((newQuestionID + 1) < totalQuestionNO)
- requestForNewQuestion(newQuestionID + 1);
- else if ((newQuestionID + 1) == totalQuestionNO) {
- showResultPage();
- }
- }
- Log.e("isSkipped", String.valueOf(isSkipped));
- Log.e("right::", String.valueOf(rightAnswerCounter));
- Log.e("wrong::", String.valueOf(wrongAnswerCounter));
- }
- });
- radioButtonOption3.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- currentQuestionIDAnswer = 3;
- Log.d("Selected text::", radioButtonOption3.getText().toString());
- //Log.d("Question answer::", answerOfQuestion(newQuestionID));
- if (isSkipped) {
- if (skipList.size() != 0) {
- //Log.d("right::", String.valueOf(answerOfQuestion(skipList.get(skippedQuentionID))));
- if (Integer.valueOf(answerOfQuestion(newQuestionID)) == currentQuestionIDAnswer) {
- //if (radioButtonOption3.getText().toString().contains(answerOfQuestion(skipList.get(skippedQuentionID)))) {
- rightAnswerCounter++;
- } else {
- wrongAnswerCounter++;
- }
- skipList.remove(skipList.get(skippedQuentionID));
- if (skipList.size() > 0)
- requestForSkippedNewQuestion(skipList.get(skippedQuentionID));
- else if (skipList.size() == 0) {
- showResultPage();
- }
- }
- } else {
- Log.e("newQuestionID", newQuestionID + "");
- try {
- if (Integer.valueOf(answerOfQuestion(newQuestionID)) == currentQuestionIDAnswer) {
- rightAnswerCounter++;
- } else {
- wrongAnswerCounter++;
- }
- } catch (Exception ignored) {
- }
- if ((newQuestionID + 1) < totalQuestionNO)
- requestForNewQuestion(newQuestionID + 1);
- else if ((newQuestionID + 1) == totalQuestionNO) {
- showResultPage();
- }
- }
- Log.e("isSkipped", String.valueOf(isSkipped));
- Log.e("right::", String.valueOf(rightAnswerCounter));
- Log.e("wrong::", String.valueOf(wrongAnswerCounter));
- }
- });
- radioButtonOption4.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- currentQuestionIDAnswer = 4;
- Log.d("Selected text::", radioButtonOption4.getText().toString());
- //Log.d("Question answer::", answerOfQuestion(newQuestionID));
- if (isSkipped) {
- if (skipList.size() != 0) {
- //Log.d("right::", String.valueOf(answerOfQuestion(skipList.get(skippedQuentionID))));
- if (Integer.valueOf(answerOfQuestion(newQuestionID)) == currentQuestionIDAnswer) {
- //if (radioButtonOption4.getText().toString().contains(answerOfQuestion(skipList.get(skippedQuentionID)))) {
- rightAnswerCounter++;
- } else {
- wrongAnswerCounter++;
- }
- skipList.remove(skipList.get(skippedQuentionID));
- if (skipList.size() > 0)
- requestForSkippedNewQuestion(skipList.get(skippedQuentionID));
- else if (skipList.size() == 0) {
- showResultPage();
- }
- }
- } else {
- Log.e("newQuestionID", newQuestionID + "");
- try {
- if (Integer.valueOf(answerOfQuestion(newQuestionID)) == currentQuestionIDAnswer) {
- rightAnswerCounter++;
- } else {
- wrongAnswerCounter++;
- }
- } catch (Exception ignored) {
- }
- if ((newQuestionID + 1) < totalQuestionNO)
- requestForNewQuestion(newQuestionID + 1);
- else if ((newQuestionID + 1) == totalQuestionNO) {
- showResultPage();
- }
- //printMessage(""+ (newQuestionID + 1));
- }
- Log.e("isSkipped", String.valueOf(isSkipped));
- Log.e("right::", String.valueOf(rightAnswerCounter));
- Log.e("wrong::", String.valueOf(wrongAnswerCounter));
- }
- });
- skipButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if (isSkipped) {
- skipList.remove(skipList.get(skippedQuentionID));
- if (skipList.size() > 0)
- if (skipList.size() == 0) {
- showResultPage();
- }
- requestForSkippedNewQuestion(skipList.get(skippedQuentionID));
- } else {
- Log.e("newQuestionID", newQuestionID + "");
- skipList.add(newQuestionID);
- requestForNewQuestion(newQuestionID + 1);
- }
- Log.e("isSkipped", String.valueOf(isSkipped));
- Log.e("right::", String.valueOf(rightAnswerCounter));
- Log.e("wrong::", String.valueOf(wrongAnswerCounter));
- }
- });
- }
- private void getModelTestData() {
- Log.e("getModelTestData", "enter");
- quizQuestionModel = ArrayListMultimap.create();
- String testData = Shared.readData("modelTestData", getApplicationContext());
- try {
- JSONArray response = new JSONArray(testData);
- totalQuestionNO = response.length();
- for (int i = 0; i < totalQuestionNO; i++) {
- JSONObject questionModel = (JSONObject) response.get(i);
- String questionID = questionModel.getString("id");
- String question = questionModel.getString("question");
- String answer = questionModel.getString("answer");
- Document formattedQuestion = Jsoup.parse(question);
- //Document formattedAnswer = Jsoup.parse(answer);
- String option1 = questionModel.getString("options1");
- String option2 = questionModel.getString("options2");
- //Log.e("option2::", option2);
- String option3 = questionModel.getString("options3");
- String option4 = questionModel.getString("options4");
- Document formattedOption1 = Jsoup.parse(option1);
- Document formattedOption2 = Jsoup.parse(option2);
- //Log.e("formattedOption2::", formattedOption2.toString());
- Document formattedOption3 = Jsoup.parse(option3);
- Document formattedOption4 = Jsoup.parse(option4);
- quizQuestionModel.put(questionID, questionID);
- quizQuestionModel.put(questionID, formattedQuestion.toString());
- quizQuestionModel.put(questionID, answer);
- quizQuestionModel.put(questionID, formattedOption1.toString());
- quizQuestionModel.put(questionID, formattedOption2.toString());
- quizQuestionModel.put(questionID, formattedOption3.toString());
- quizQuestionModel.put(questionID, formattedOption4.toString());
- }
- if (!testData.isEmpty()) {
- requestForNewQuestion(newQuestionID);
- } else {
- Toast.makeText(getApplicationContext(), "No data loaded!", Toast.LENGTH_LONG).show();
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- public String answerOfQuestion(int questionID) {
- String answer;
- Set<String> keys = quizQuestionModel.keySet();
- // Putting Set of keys to an arraylist
- List<String> keyStr_list = new ArrayList<>(keys);
- //Initializing an Integer arraylist having the same size of its ancestor String arraylist
- List<Integer> keyInt_list = new ArrayList<>(keyStr_list.size());
- // Putting values of String arraylist to Integer Arraylist
- for (int i = 0; i < keyStr_list.size(); i++) {
- keyInt_list.add(Integer.parseInt(keyStr_list.get(i)));
- }
- // Sort the Integer arraylist from small to larger values
- Collections.sort(keyInt_list);
- //if (keys.contains(String.valueOf(questionID))) {
- Object[] questionObject = quizQuestionModel.get(String.valueOf(keyInt_list.get(questionID))).toArray();
- answer = questionObject[2].toString();
- //}
- Log.e("answer", answer);
- return answer;
- }
- @SuppressLint("SetTextI18n")
- public void requestForNewQuestion(int questionID) {
- resetOptionsButton();
- // get key(i.e. Question ID) from quizQuestionModel
- Set<String> keys = quizQuestionModel.keySet();
- Log.e("question keys", keys.toString());
- // Putting Set of keys to an arraylist
- List<String> keyStr_list = new ArrayList<>(keys);
- //Initializing an Integer arraylist having the same size of its ancestor String arraylist
- List<Integer> keyInt_list = new ArrayList<>(keyStr_list.size());
- // Putting values of String arraylist to Integer Arraylist
- for (int i = 0; i < keyStr_list.size(); i++) {
- keyInt_list.add(Integer.parseInt(keyStr_list.get(i)));
- }
- // Sort the Integer arraylist from small to larger values
- Collections.sort(keyInt_list);
- //Log.e("question id", questionID + "");
- //Log.e("question id value", String.valueOf(keyInt_list.get(questionID)));
- if ((newQuestionID + 1) > keys.size()) {
- Log.e("skipped questions id::", String.valueOf(skipList));
- if (skipList.size() != 0) {
- // Intent intent = new Intent(MainActivity.this, SkipActivity1.class);
- // intent.putIntegerArrayListExtra("skip_question_id", skipList);
- // intent.putExtra("quizQuestionModel", (Serializable) quizQuestionModel);
- // //intent.putExtra("skip_question_id", skipList);
- // startActivity(intent);
- // finish();
- isSkipped = true;
- skippedQuentionID = 0;
- requestForSkippedNewQuestion(skipList.get(skippedQuentionID));
- } else {
- showResultPage();
- }
- }
- //if (keys.contains(String.valueOf(questionID))) {
- Object[] questionObject = quizQuestionModel.get(String.valueOf(keyInt_list.get(questionID))).toArray();
- newQuestionID = questionID; //Integer.valueOf(questionObject[0].toString());
- textviewQuestionNo.setText((newQuestionID + 1) + "/" + totalQuestionNO/*keys.size()*/);
- //Log.e("question", questionObject.toString());
- //question
- textviewQuestion.setText(Html.fromHtml(questionObject[1].toString()));
- //answer option
- radioButtonOption1.setText(Html.fromHtml(questionObject[3].toString()));
- radioButtonOption2.setText(Html.fromHtml(questionObject[4].toString()));
- radioButtonOption3.setText(Html.fromHtml(questionObject[5].toString()));
- radioButtonOption4.setText(Html.fromHtml(questionObject[6].toString()));
- //}
- }
- public void showResultPage() {
- Intent intent = new Intent(ModelTestActivity.this, ResultActivity.class);
- intent.putExtra("test_id", test_id);
- intent.putExtra("rightAnswer", rightAnswerCounter);
- intent.putExtra("wrongAnswer", wrongAnswerCounter);
- startActivity(intent);
- finish();
- }
- @SuppressLint("SetTextI18n")
- public void requestForSkippedNewQuestion(int questionID) {
- resetOptionsButton();
- Set<String> keys = quizQuestionModel.keySet();
- if (keys.contains(String.valueOf(questionID))) {
- Object[] questionObject = quizQuestionModel.get(String.valueOf(questionID)).toArray();
- newQuestionID = Integer.valueOf(questionObject[0].toString());
- textviewQuestionNo.setText((newQuestionID + 1) + "/" + keys.size());
- //question
- textviewQuestion.setText(Html.fromHtml(questionObject[1].toString()));
- //answer option
- radioButtonOption1.setText(Html.fromHtml(questionObject[3].toString()));
- radioButtonOption2.setText(Html.fromHtml(questionObject[4].toString()));
- radioButtonOption3.setText(Html.fromHtml(questionObject[5].toString()));
- radioButtonOption4.setText(Html.fromHtml(questionObject[6].toString()));
- }
- }
- private void resetOptionsButton() {
- radioButtonOption1.setChecked(false);
- radioButtonOption2.setChecked(false);
- radioButtonOption3.setChecked(false);
- radioButtonOption4.setChecked(false);
- }
- private void getSkipQuestion(int questionID) {
- {
- Object[] questionObject = quizQuestionModel.get(String.valueOf(questionID)).toArray();
- newQuestionID = Integer.valueOf(questionObject[0].toString());
- //question
- textviewQuestion.setText(Html.fromHtml(questionObject[1].toString()));
- //answer option
- radioButtonOption1.setText(Html.fromHtml(questionObject[3].toString()));
- radioButtonOption2.setText(Html.fromHtml(questionObject[4].toString()));
- radioButtonOption3.setText(Html.fromHtml(questionObject[5].toString()));
- radioButtonOption4.setText(Html.fromHtml(questionObject[6].toString()));
- }
- }
- private boolean isOptionButtonPressed() {
- boolean isPressed = false;
- if (radioButtonOption1.isChecked() || radioButtonOption2.isChecked() || radioButtonOption3.isChecked() || radioButtonOption4.isChecked())
- isPressed = true;
- return isPressed;
- }
- private void printMessage(String message) {
- Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
- }
- @Override
- public void onDestroy() {
- timer.cancel();
- super.onDestroy();
- }
- public class CounterClass extends CountDownTimer {
- CounterClass(long millisInFuture, long countDownInterval) {
- super(millisInFuture, countDownInterval);
- }
- @Override
- public void onFinish() {
- Intent intent = new Intent(ModelTestActivity.this, ResultActivity.class);
- intent.putExtra("test_id", test_id);
- intent.putExtra("rightAnswer", rightAnswerCounter);
- intent.putExtra("wrongAnswer", wrongAnswerCounter);
- startActivity(intent);
- finish();
- }
- @Override
- public void onTick(long millisUntilFinished) {
- @SuppressLint("DefaultLocale")
- String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millisUntilFinished),
- TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
- TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)));
- System.out.println(hms);
- textTime.setText(hms);
- }
- }
- @Override
- public boolean onSupportNavigateUp() {
- onBackPressed();
- return true;
- }
- @Override
- public void onBackPressed() {
- timer.cancel();
- super.onBackPressed();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement