Advertisement
Guest User

Ashwath's QuizApp MainActivity

a guest
May 16th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 19.53 KB | None | 0 0
  1. package com.example.chitraramaswamy.quizapp;
  2.  
  3. import android.content.Context;
  4. import android.content.DialogInterface;
  5. import android.icu.util.Output;
  6. import android.support.v4.app.Fragment;
  7. import android.support.v4.app.FragmentManager;
  8. import android.support.v4.app.FragmentTransaction;
  9. import android.support.v4.widget.TextViewCompat;
  10. import android.support.v7.app.AlertDialog;
  11. import android.support.v7.app.AppCompatActivity;
  12. import android.os.Bundle;
  13. import android.util.Log;
  14. import android.view.View;
  15. import android.widget.Button;
  16. import android.widget.RadioGroup;
  17. import android.widget.TextView;
  18.  
  19. import java.io.BufferedReader;
  20. import java.io.InputStreamReader;
  21. import java.io.OutputStream;
  22. import java.io.OutputStreamWriter;
  23. import java.util.ArrayList;
  24.  
  25. public class MainActivity extends AppCompatActivity {
  26.  
  27.     FragmentTransaction fragmentTransaction;
  28.     ArrayList<Bundle> questions;
  29.     Button nextButton;
  30.     Button prevButton;
  31.     Button submitButton;
  32.     TextView progressTextView;
  33.     int currentQuestion = 0;
  34.     ArrayList<String> userAnswers;
  35.     ArrayList<String> correctAnswers;
  36.     int score = 0;
  37.     FragmentManager fragmentManager;
  38.     Button saveButton;
  39.     Button loadButton;
  40.     String file;
  41.  
  42.     @Override
  43.     protected void onCreate(Bundle savedInstanceState) {
  44.         super.onCreate(savedInstanceState);
  45.         setContentView(R.layout.activity_main);
  46.         file = "content.txt";
  47.         nextButton = (Button) findViewById(R.id.nextQuestionButton);
  48.         prevButton = (Button) findViewById(R.id.previousQuestionButton);
  49.         prevButton.setClickable(false);
  50.         submitButton = (Button) findViewById(R.id.submitButton);
  51.         saveButton = (Button) findViewById(R.id.saveAndQuitButton);
  52.         progressTextView = (TextView) findViewById(R.id.scoreTextView);
  53.         loadButton = (Button) findViewById(R.id.loadButton);
  54.  
  55.         fragmentManager = getSupportFragmentManager();
  56.         fragmentTransaction = fragmentManager.beginTransaction();
  57.  
  58.         questions = new ArrayList<>();
  59.         userAnswers = new ArrayList<>();
  60.         correctAnswers = new ArrayList<>();
  61.  
  62.         for(int i = 0; i < 10; i++){
  63.             userAnswers.add(null);
  64.         }
  65.  
  66.  
  67.         Bundle q1Bundle = new Bundle();
  68.         q1Bundle.putString("Question", "In what year was Counter Strike: Global Offensive's first 'Major'?");
  69.         q1Bundle.putString("ChoiceA", "2006");
  70.         q1Bundle.putString("ChoiceB", "2012");
  71.         q1Bundle.putString("ChoiceC", "2014");
  72.         q1Bundle.putString("ChoiceD", "2013");
  73.         q1Bundle.putString("CorrectChoice", "2013");
  74.  
  75.         Bundle q2Bundle = new Bundle();
  76.         q2Bundle.putString("Question", "Which tournament organizer organized the first CS:GO Major of 2017?");
  77.         q2Bundle.putString("ChoiceA", "ESL");
  78.         q2Bundle.putString("ChoiceB", "MLG");
  79.         q2Bundle.putString("ChoiceC", "ELEAGUE");
  80.         q2Bundle.putString("ChoiceD", "Dreamhack");
  81.         q2Bundle.putString("CorrectChoice", "ELEAGUE");
  82.  
  83.         Bundle q3Bundle = new Bundle();
  84.         q3Bundle.putString("Question", "The following famous quote was said by which CS:GO caster?: 'Let's not count him out of this, we're in the Big Apple and Snax is hungry'");
  85.         q3Bundle.putString("ChoiceA", "Anders Blume");
  86.         q3Bundle.putString("ChoiceB", "Auguste 'Semmler' Massonat");
  87.         q3Bundle.putString("ChoiceC", "Matthew 'Sadokist' Trivett");
  88.         q3Bundle.putString("ChoiceD", "James 'JZFB' Bardolph");
  89.         q3Bundle.putString("CorrectChoice", "Matthew 'Sadokist' Trivett");
  90.  
  91.         Bundle q4Bundle = new Bundle();
  92.         q4Bundle.putString("Question", "With the addition of 's1mple' to their starting lineup, Natus Vincere have won 1 tournament. Which?");
  93.         q4Bundle.putString("ChoiceA", "CounterPit Season 2");
  94.         q4Bundle.putString("ChoiceB", "Dreamhack Leipzig 2017");
  95.         q4Bundle.putString("ChoiceC", "ESL One New York 2016");
  96.         q4Bundle.putString("ChoiceD", "StarSeries i-League Starladder Season 3: Kiev");
  97.         q4Bundle.putString("CorrectChoice", "ESL One New York 2016");
  98.  
  99.         Bundle q5Bundle = new Bundle();
  100.         q5Bundle.putString("Question", "Which of the following players was named HLTV's #1 player of 2016?");
  101.         q5Bundle.putString("ChoiceA", "Coldzera (SK Gaming)");
  102.         q5Bundle.putString("ChoiceB", "NiKo (Mousesports)");
  103.         q5Bundle.putString("ChoiceC", "FalleN (SK Gaming)");
  104.         q5Bundle.putString("ChoiceD", "Olofmeister (Fnatic)");
  105.         q5Bundle.putString("CorrectChoice", "Coldzera (SK Gaming)");
  106.  
  107.         Bundle q6Bundle = new Bundle();
  108.         q6Bundle.putString("Question", "True or False: ESL One Cologne 2017 will be a Major");
  109.         q6Bundle.putBoolean("CorrectChoice", false);
  110.  
  111.         Bundle q7Bundle = new Bundle();
  112.         q7Bundle.putString("Question", "True or False: KennyS is the AWPer of G2 eSports");
  113.         q7Bundle.putBoolean("CorrectChoice", true);
  114.  
  115.         Bundle q8Bundle = new Bundle();
  116.         q8Bundle.putString("Question", "True or False: Astralis won ELEAGUE Major 2017");
  117.         q8Bundle.putBoolean("CorrectChoice", true);
  118.  
  119.         Bundle q9Bundle = new Bundle();
  120.         q9Bundle.putString("Question", "True or False: the first CS:GO event to be broadcast live on American telivision was the ELEAGUE Major");
  121.         q9Bundle.putBoolean("CorrectChoice", false);
  122.  
  123.         Bundle q10Bundle = new Bundle();
  124.         q10Bundle.putString("Question", "True or False: the first dominant team in CS:GO was Ninjas in Pyjamas, who started their CS:GO campaign with an 87 game winning streak on LAN");
  125.         q10Bundle.putBoolean("CorrectChoice", true);
  126.  
  127.         questions.add(q1Bundle);
  128.         questions.add(q2Bundle);
  129.         questions.add(q3Bundle);
  130.         questions.add(q4Bundle);
  131.         questions.add(q5Bundle);
  132.         questions.add(q6Bundle);
  133.         questions.add(q7Bundle);
  134.         questions.add(q8Bundle);
  135.         questions.add(q9Bundle);
  136.         questions.add(q10Bundle);
  137.  
  138.         for(int i = 0;  i < questions.size(); i++){
  139.             if(i <= 4) {
  140.                 correctAnswers.add((String) questions.get(i).get("CorrectChoice"));
  141.             }
  142.             else{
  143.                 boolean a = (Boolean) questions.get(i).get("CorrectChoice");
  144.                 String x;
  145.                 if(a){
  146.                     x = "TRUE";
  147.                 }
  148.                 else{
  149.                     x = "FALSE";
  150.                 }
  151.                 correctAnswers.add(x);
  152.             }
  153.         }
  154.  
  155.         fragmentTransaction = fragmentManager.beginTransaction();
  156.         final multipleChoice fragment = new multipleChoice();
  157.         fragment.setArguments(questions.get(0));
  158.         fragmentTransaction.add(R.id.linearLayout, fragment);
  159.         fragmentTransaction.commit();
  160.  
  161.         nextButton.setOnClickListener(new View.OnClickListener() {
  162.             @Override
  163.             public void onClick(View view) {
  164.                 if(currentQuestion < 9) {
  165.                     if(currentQuestion <= 4){
  166.                         multipleChoice currentFragment = (multipleChoice) fragmentManager.findFragmentById(R.id.linearLayout);
  167.                         userAnswers.set(currentQuestion, currentFragment.selectedAnswer);
  168.                     }
  169.                     else{
  170.                         trueFalse currentFragment = (trueFalse) fragmentManager.findFragmentById(R.id.linearLayout);
  171.                         userAnswers.set(currentQuestion, currentFragment.selectedAnswer);
  172.                     }
  173.                     if(currentQuestion != 9) {
  174.                         currentQuestion++;
  175.                     }
  176.                 }
  177.  
  178.                 selectUserAnswer();
  179.  
  180.                 if(currentQuestion <= 4){
  181.                     multipleChoice question = new multipleChoice();
  182.                     question.setArguments(questions.get(currentQuestion));
  183.                     fragmentTransaction = fragmentManager.beginTransaction();
  184.                     fragmentTransaction.replace(R.id.linearLayout, question);
  185.                 }
  186.                 else{
  187.                     trueFalse question = new trueFalse();
  188.                     question.setArguments(questions.get(currentQuestion));
  189.                     fragmentTransaction = fragmentManager.beginTransaction();
  190.                     fragmentTransaction.replace(R.id.linearLayout, question);
  191.                 }
  192.  
  193.                 fragmentTransaction.commit();
  194.  
  195.                 selectUserAnswer();
  196.  
  197.                 if(currentQuestion == 9){
  198.                     nextButton.setClickable(false);
  199.                 }
  200.                 else{
  201.                     nextButton.setClickable(true);
  202.                 }
  203.  
  204.                 if(currentQuestion == 0){
  205.                     prevButton.setClickable(false);
  206.                 }
  207.                 else{
  208.                     prevButton.setClickable(true);
  209.                 }
  210.             }
  211.         });
  212.  
  213.         prevButton.setOnClickListener(new View.OnClickListener() {
  214.             @Override
  215.             public void onClick(View view) {
  216.                 if(currentQuestion > 0) {
  217.                     if(currentQuestion <= 4){
  218.                         multipleChoice currentFragment = (multipleChoice) fragmentManager.findFragmentById(R.id.linearLayout);
  219.                         userAnswers.set(currentQuestion, currentFragment.selectedAnswer);
  220.                     }
  221.                     else{
  222.                         trueFalse currentFragment = (trueFalse) fragmentManager.findFragmentById(R.id.linearLayout);
  223.                         userAnswers.set(currentQuestion, currentFragment.selectedAnswer);
  224.                     }
  225.  
  226.                     if(currentQuestion != 0) {
  227.                         currentQuestion--;
  228.                     }
  229.                 }
  230.  
  231.                 selectUserAnswer();
  232.  
  233.                 if(currentQuestion <= 4){
  234.                     multipleChoice question = new multipleChoice();
  235.                     question.setArguments(questions.get(currentQuestion));
  236.                     fragmentTransaction = fragmentManager.beginTransaction();
  237.                     fragmentTransaction.replace(R.id.linearLayout, question);
  238.                 }
  239.                 else{
  240.                     trueFalse question = new trueFalse();
  241.                     question.setArguments(questions.get(currentQuestion));
  242.                     fragmentTransaction = fragmentManager.beginTransaction();
  243.                     fragmentTransaction.replace(R.id.linearLayout, question);
  244.                 }
  245.  
  246.                 fragmentTransaction.commit();
  247.  
  248.                 //selectUserAnswer();
  249.  
  250.                 if(currentQuestion == 9){
  251.                     nextButton.setClickable(false);
  252.                 }
  253.                 else{
  254.                     nextButton.setClickable(true);
  255.                 }
  256.  
  257.                 if(currentQuestion == 0){
  258.                     prevButton.setClickable(false);
  259.                 }
  260.                 else{
  261.                     nextButton.setClickable(true);
  262.                 }
  263.             }
  264.         });
  265.  
  266.         submitButton.setOnClickListener(new View.OnClickListener() {
  267.             @Override
  268.             public void onClick(View view) {
  269.  
  270.                 if(currentQuestion <= 4){
  271.                     multipleChoice currentFragment = (multipleChoice) fragmentManager.findFragmentById(R.id.linearLayout);
  272.                     userAnswers.set(currentQuestion, currentFragment.selectedAnswer);
  273.                 }
  274.                 else{
  275.                     trueFalse currentFragment = (trueFalse) fragmentManager.findFragmentById(R.id.linearLayout);
  276.                     userAnswers.set(currentQuestion, currentFragment.selectedAnswer);
  277.                 }
  278.  
  279.                 for(int x = 0; x < userAnswers.size(); x++){
  280.                     if(userAnswers.get(x) != null) {
  281.                         if (userAnswers.get(x).toUpperCase().equals(correctAnswers.get(x).toUpperCase())) {
  282.                             score++;
  283.                         }
  284.                     }
  285.                 }
  286.                 progressTextView.setText("Score: " + score);
  287.  
  288.                 AlertDialog.Builder popUpWindow = new AlertDialog.Builder(MainActivity.this);
  289.                 popUpWindow.setTitle("Game over! Your score was " + score +"/10");
  290.                 CharSequence [] options = new CharSequence[] {"Play again", "Quit"};
  291.                 popUpWindow.setItems(options, new DialogInterface.OnClickListener() {
  292.                     @Override
  293.                     public void onClick(DialogInterface dialogInterface, int i) {
  294.                         switch(i) {
  295.                             case 0:
  296.                                 fragmentTransaction = fragmentManager.beginTransaction();
  297.                                 final multipleChoice fragment = new multipleChoice();
  298.                                 fragment.setArguments(questions.get(0));
  299.                                 fragmentTransaction.replace(R.id.linearLayout, fragment);
  300.                                 fragmentTransaction.commit();
  301.                                 currentQuestion = 0;
  302.                                 progressTextView.setText("Previous Score: " + score);
  303.                                 score = 0;
  304.                                 nextButton.setClickable(true);
  305.                                 prevButton.setClickable(false);
  306.                                 for(int x = 0; x < userAnswers.size(); x++){
  307.                                     userAnswers.set(x, null);
  308.                                 }
  309.                                 break;
  310.                             case 1:
  311.                                 MainActivity.this.finishAffinity();
  312.                                 break;
  313.                         }
  314.                     }
  315.                 });
  316.                 popUpWindow.show();
  317.             }
  318.         });
  319.  
  320.         saveButton.setOnClickListener(new View.OnClickListener() {
  321.             @Override
  322.             public void onClick(View view) {
  323.                 if(currentQuestion <= 4){
  324.                     multipleChoice currentFragment = (multipleChoice) fragmentManager.findFragmentById(R.id.linearLayout);
  325.                     userAnswers.set(currentQuestion, currentFragment.selectedAnswer);
  326.                 }
  327.                 else{
  328.                     trueFalse currentFragment = (trueFalse) fragmentManager.findFragmentById(R.id.linearLayout);
  329.                     userAnswers.set(currentQuestion, currentFragment.selectedAnswer);
  330.                 }
  331.                 try{
  332.                     OutputStreamWriter writer = new OutputStreamWriter(openFileOutput(file, Context.MODE_PRIVATE));
  333.                     for(int x = 0; x < userAnswers.size(); x++){
  334.                         if(userAnswers.get(x) == null){
  335.                             writer.write("NULL" + "\n");
  336.                         }
  337.                         else {
  338.                             writer.write(userAnswers.get(x) + "\n");
  339.                         }
  340.                     }
  341.                     writer.close();
  342.                 }
  343.                 catch(Exception e){
  344.                     Log.d("DEBUGGING", e.getLocalizedMessage());
  345.                 }
  346.  
  347.                 //MainActivity.this.finishAffinity();
  348.             }
  349.         });
  350.  
  351.         loadButton.setOnClickListener(new View.OnClickListener() {
  352.             @Override
  353.             public void onClick(View view) {
  354.                 try{
  355.                     BufferedReader reader = new BufferedReader(new InputStreamReader(openFileInput(file)));
  356.                     for(int x = 0; x < userAnswers.size(); x++){
  357.                         String line = reader.readLine();
  358.                         if(!(line.toUpperCase().equals("NULL"))){
  359.                             userAnswers.set(x, line);
  360.                         }
  361.                     }
  362.                     reader.close();
  363.                 }
  364.                 catch(Exception e){
  365.                     e.printStackTrace();
  366.                 }
  367.  
  368.                 if(currentQuestion <= 4){
  369.                     multipleChoice currentFragment = (multipleChoice) fragmentManager.findFragmentById(R.id.linearLayout);
  370.                     if(userAnswers.get(currentQuestion) != null){
  371.                         if(userAnswers.get(currentQuestion).toUpperCase().equals(currentFragment.answerA.toUpperCase())){
  372.                             currentFragment.choiceA.setChecked(true);
  373.                         }
  374.                         else if(userAnswers.get(currentQuestion).toUpperCase().equals(currentFragment.answerB.toUpperCase())){
  375.                             currentFragment.choiceB.setChecked(true);
  376.                         }
  377.                         else if(userAnswers.get(currentQuestion).toUpperCase().equals(currentFragment.answerC.toUpperCase())){
  378.                             currentFragment.choiceC.setChecked(true);
  379.                         }
  380.                         else if(userAnswers.get(currentQuestion).toUpperCase().equals(currentFragment.answerD.toUpperCase())){
  381.                             currentFragment.choiceD.setChecked(true);
  382.                         }
  383.                     }
  384.                 }
  385.                 else{
  386.                     trueFalse currentFragment = (trueFalse) fragmentManager.findFragmentById(R.id.linearLayout);
  387.                     if(userAnswers.get(currentQuestion) != null){
  388.                         if(userAnswers.get(currentQuestion).toUpperCase().equals("TRUE")){
  389.                             currentFragment.trueButton.setChecked(true);
  390.                         }
  391.                         else if(userAnswers.get(currentQuestion).toUpperCase().equals("FALSE")){
  392.                             currentFragment.falseButton.setChecked(true);
  393.                         }
  394.                     }
  395.                 }
  396.  
  397.                 try{
  398.                     OutputStreamWriter writer = new OutputStreamWriter(openFileOutput(file, Context.MODE_PRIVATE));
  399.                     writer.write("");
  400.                     writer.close();
  401.                 }
  402.                 catch(Exception e){
  403.  
  404.                 }
  405.  
  406.             }
  407.         });
  408.  
  409.     }
  410.  
  411.     public void selectUserAnswer(){
  412.         /**if(userAnswers.get(currentQuestion) != null){
  413.             String userSelected = userAnswers.get(currentQuestion);
  414.             if(currentQuestion <= 4){
  415.                 multipleChoice currentFragment = (multipleChoice) fragmentManager.findFragmentById(R.id.linearLayout);
  416.                 if(userSelected.toUpperCase().equals(currentFragment.answerA.toUpperCase())){
  417.                     currentFragment.choiceA.setSelected(true);
  418.                 }
  419.                 else if(userSelected.toUpperCase().equals(currentFragment.answerB.toUpperCase())){
  420.                     currentFragment.choiceB.setSelected(true);
  421.                 }
  422.                 else if(userSelected.toUpperCase().equals(currentFragment.answerC.toUpperCase())){
  423.                     currentFragment.choiceC.setSelected(true);
  424.                 }
  425.                 else if(userSelected.toUpperCase().equals(currentFragment.answerD.toUpperCase())){
  426.                     currentFragment.choiceD.setSelected(true);
  427.                 }
  428.             }
  429.             else{
  430.                 trueFalse currentFragment = (trueFalse) fragmentManager.findFragmentById(R.id.linearLayout);
  431.                 if(userSelected.toUpperCase().equals("TRUE")){
  432.                     currentFragment.trueButton.setSelected(true);
  433.                 }
  434.                 else if(userSelected.toUpperCase().equals("FALSE")){
  435.                     currentFragment.falseButton.setSelected(true);
  436.                 }
  437.             }
  438.         }*/
  439.         if(userAnswers.get(currentQuestion) != null){
  440.             questions.get(currentQuestion).putString("selectedAnswer", userAnswers.get(currentQuestion));
  441.         }
  442.     }
  443. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement