Advertisement
pabloducato

QuizOnlineActivity

Jul 6th, 2019
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.46 KB | None | 0 0
  1. package com.example.quiz.AccountActivity;
  2.  
  3.  
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.graphics.Color;
  7. import android.net.ConnectivityManager;
  8. import android.net.NetworkInfo;
  9. import android.os.Bundle;
  10. import android.os.CountDownTimer;
  11. import android.os.Handler;
  12. import android.support.annotation.NonNull;
  13. import android.support.annotation.Nullable;
  14. import android.support.v7.app.AppCompatActivity;
  15. import android.view.View;
  16. import android.widget.Button;
  17. import android.widget.TextView;
  18. import android.widget.Toast;
  19.  
  20. import com.example.quiz.AccountActivity.Model.QuestionFirebase;
  21. import com.example.quiz.R;
  22. import com.google.firebase.database.DataSnapshot;
  23. import com.google.firebase.database.DatabaseError;
  24. import com.google.firebase.database.DatabaseReference;
  25. import com.google.firebase.database.FirebaseDatabase;
  26. import com.google.firebase.database.ValueEventListener;
  27.  
  28. public class QuizOnlineActivity extends AppCompatActivity {
  29.  
  30.     Button beback,b1,b2,b3,b4,accept,reset;
  31.     TextView question, timer, maintitle;
  32.     private CountDownTimer countDownTimer;
  33.  
  34.  
  35.     int total = 1;
  36.     int questiontag = 0;
  37.     int correct = 0;
  38.     int wrong = 0;
  39.  
  40.  
  41.     DatabaseReference databaseReference;
  42.  
  43.     @Override
  44.     protected void onCreate(Bundle savedInstanceState) {
  45.         super.onCreate(savedInstanceState);
  46.         setContentView(R.layout.activity_quiz_online);
  47.  
  48.         b1 = findViewById(R.id.button1);
  49.         b2 = findViewById(R.id.button2);
  50.         b3 = findViewById(R.id.button3);
  51.         b4 = findViewById(R.id.button4);
  52.  
  53.         question = findViewById(R.id.question);
  54.         timer = findViewById(R.id.timer);
  55.         maintitle = findViewById(R.id.maintitle);
  56.  
  57.         accept = findViewById(R.id.accept);
  58.  
  59.         reset = findViewById(R.id.reset);
  60.  
  61.         beback = findViewById(R.id.beback);
  62.         beback.setOnClickListener(new View.OnClickListener() {
  63.             @Override
  64.             public void onClick(View v) {
  65.                 Intent intent = new Intent(QuizOnlineActivity.this, ProfileActivity.class);
  66.                 startActivity(intent);
  67.             }
  68.         });
  69.  
  70.         accept.setOnClickListener(new View.OnClickListener() {
  71.             @Override
  72.             public void onClick(View v) {
  73.                 maintitle.setVisibility(v.GONE);
  74.                 accept.setText("Potwierdź");
  75.                 beback.setText("Przerwij");
  76.                 b1.setVisibility(v.VISIBLE);
  77.                 b2.setVisibility(v.VISIBLE);
  78.                 b3.setVisibility(v.VISIBLE);
  79.                 b4.setVisibility(v.VISIBLE);
  80.                 question.setVisibility(v.VISIBLE);
  81.                 timer.setVisibility(v.VISIBLE);
  82.                 reset.setVisibility(v.VISIBLE);
  83.                 updateQuestions();
  84.             }
  85.         });
  86.  
  87.     }
  88.  
  89.     private boolean isNetworkAvailable() {
  90.         ConnectivityManager connectivityManager
  91.                 = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  92.         NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
  93.         return activeNetworkInfo != null && activeNetworkInfo.isConnected();
  94.     }
  95.  
  96.     private void updateQuestions()
  97.     {
  98.         if(isNetworkAvailable()==false)
  99.         {
  100.             Toast.makeText(QuizOnlineActivity.this, "Brak połączenia z internetem", Toast.LENGTH_SHORT).show();
  101.  
  102.         }
  103.         else if(isNetworkAvailable()==true && total==31)
  104.         {
  105.             Intent i = new Intent(getApplicationContext(), ResultActivity.class);
  106.             i.putExtra("Łącznie pytań:", String.valueOf(questiontag));
  107.             i.putExtra("Poprawne odpowiedzi:", String.valueOf(correct));
  108.             i.putExtra("Złe odpowiedzi:", String.valueOf(wrong));
  109.             startActivity(i);
  110.         }
  111.         else
  112.         {
  113.             databaseReference = FirebaseDatabase.getInstance().getReference().child("questions").child(String.valueOf(total));
  114.             databaseReference.addValueEventListener(new ValueEventListener() {
  115.                 @Override
  116.                 public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  117.                     final QuestionFirebase questionFirebase = dataSnapshot.getValue(QuestionFirebase.class);
  118.  
  119.                     question.setText(questionFirebase.getQuestion());
  120.                     b1.setText(questionFirebase.getOption1());
  121.                     b2.setText(questionFirebase.getOption2());
  122.                     b3.setText(questionFirebase.getOption3());
  123.                     b4.setText(questionFirebase.getOption4());
  124.  
  125.                     //final boolean[] tmp1 = new boolean[1];
  126.                     //final boolean[] tmp2 = new boolean[1];
  127.                     //final boolean[] tmp3 = new boolean[1];
  128.                     //final boolean[] tmp4 = new boolean[1];
  129.  
  130.                     //tmp1[0]=true;
  131.                     //tmp2[0]=true;
  132.                     //tmp3[0]=true;
  133.                     //tmp4[0]=true;
  134.  
  135.                     //final String[] s1 = {String.valueOf(tmp1)};
  136.                     //final String[] s2 = {String.valueOf(tmp2)};
  137.                     //final String[] s3 = {String.valueOf(tmp4)};
  138.                     //final String[] s4 = {String.valueOf(tmp4)};
  139.  
  140.                     final boolean[] varControlOne = {true};
  141.                     final boolean[] varControlTwo = {true};
  142.                     final boolean[] varControlThree = {true};
  143.                     final boolean[] varControlFour = {true};
  144.  
  145.  
  146.                     if(b1.getText().toString().equals(questionFirebase.getAnswer1()))
  147.                     {
  148.                         varControlOne[0] = false;
  149.                     }
  150.                     else
  151.                     {
  152.                         varControlOne[0] = true;
  153.                     }
  154.                     if(b2.getText().toString().equals(questionFirebase.getAnswer2()))
  155.                     {
  156.                         varControlTwo[0] = false;
  157.                     }
  158.                     else
  159.                     {
  160.                         varControlTwo[0] = true;
  161.                     }
  162.                     if(b3.getText().toString().equals(questionFirebase.getAnswer3()))
  163.                     {
  164.                         varControlThree[0] = false;
  165.                     }
  166.                     else
  167.                     {
  168.                         varControlThree[0] = true;
  169.                     }
  170.                     if(b4.getText().toString().equals(questionFirebase.getAnswer4()))
  171.                     {
  172.                         varControlFour[0] = false;
  173.                     }
  174.                     else
  175.                     {
  176.                         varControlFour[0] = true;
  177.                     }
  178.  
  179.  
  180.                     b1.setOnClickListener(new View.OnClickListener() {
  181.                         @Override
  182.                         public void onClick(View view) {
  183.                             b1.setBackgroundColor(Color.YELLOW);
  184.                             if(b1.getText().toString().equals(questionFirebase.getAnswer1()))
  185.                             {
  186.                                 varControlOne[0] = true;
  187.                             }
  188.                         }
  189.                     });
  190.  
  191.                     b2.setOnClickListener(new View.OnClickListener() {
  192.                         @Override
  193.                         public void onClick(View v) {
  194.                             b2.setBackgroundColor(Color.YELLOW);
  195.                             if(b2.getText().toString().equals(questionFirebase.getAnswer2()))
  196.                             {
  197.                                 varControlTwo[0] = true;
  198.                             }
  199.  
  200.                         }
  201.                     });
  202.  
  203.                     b3.setOnClickListener(new View.OnClickListener() {
  204.                         @Override
  205.                         public void onClick(View v) {
  206.                             b3.setBackgroundColor(Color.YELLOW);
  207.                             if(b3.getText().toString().equals(questionFirebase.getAnswer3()))
  208.                             {
  209.                                 varControlThree[0] = true;
  210.                             }
  211.                         }
  212.                     });
  213.  
  214.                     b4.setOnClickListener(new View.OnClickListener() {
  215.                         @Override
  216.                         public void onClick(View v) {
  217.                             b4.setBackgroundColor(Color.YELLOW);
  218.                             if(b4.getText().toString().equals(questionFirebase.getAnswer4()))
  219.                             {
  220.                                 varControlFour[0] = true;
  221.                             }
  222.                         }
  223.                     });
  224.  
  225.                     reset.setOnClickListener(new View.OnClickListener() {
  226.                         @Override
  227.                         public void onClick(View v) {
  228.                             b1.setBackgroundColor(Color.WHITE);
  229.                             b2.setBackgroundColor(Color.WHITE);
  230.                             b3.setBackgroundColor(Color.WHITE);
  231.                             b4.setBackgroundColor(Color.WHITE);
  232.                             varControlOne[0] = true;
  233.                             varControlTwo[0] = true;
  234.                             varControlThree[0] = true;
  235.                             varControlFour[0] = true;
  236.                             if(b1.getText().toString().equals(questionFirebase.getAnswer1()))
  237.                             {
  238.                                 varControlOne[0] = false;
  239.                             }
  240.                             else
  241.                             {
  242.                                 varControlOne[0] = true;
  243.                             }
  244.                             if(b2.getText().toString().equals(questionFirebase.getAnswer2()))
  245.                             {
  246.                                 varControlTwo[0] = false;
  247.                             }
  248.                             else
  249.                             {
  250.                                 varControlTwo[0] = true;
  251.                             }
  252.                             if(b3.getText().toString().equals(questionFirebase.getAnswer3()))
  253.                             {
  254.                                 varControlThree[0] = false;
  255.                             }
  256.                             else
  257.                             {
  258.                                 varControlThree[0] = true;
  259.                             }
  260.                             if(b4.getText().toString().equals(questionFirebase.getAnswer4()))
  261.                             {
  262.                                 varControlFour[0] = false;
  263.                             }
  264.                             else
  265.                             {
  266.                                 varControlFour[0] = true;
  267.                             }
  268.                         }
  269.                     });
  270.  
  271.  
  272.                     accept.setOnClickListener(new View.OnClickListener() {
  273.                             @Override
  274.                             public void onClick(View v) {
  275.                                 if(varControlOne[0] == true && varControlTwo[0] == true && varControlThree[0] == true && varControlFour[0]==true)
  276.                                 {
  277.                                     correct++;
  278.                                     questiontag++;
  279.                                     Handler handler = new Handler();
  280.                                     handler.postDelayed(new Runnable() {
  281.                                         @Override
  282.                                         public void run() {
  283.                                             //countDownTimer.cancel();
  284.                                             b1.setBackgroundColor(Color.parseColor("#FFFFFF"));
  285.                                             b2.setBackgroundColor(Color.parseColor("#FFFFFF"));
  286.                                             b3.setBackgroundColor(Color.parseColor("#FFFFFF"));
  287.                                             b4.setBackgroundColor(Color.parseColor("#FFFFFF"));
  288.                                             updateQuestions();
  289.                                         }
  290.                                     },0);
  291.                                 }
  292.                                 else
  293.                                 {
  294.                                     wrong++;
  295.                                     questiontag++;
  296.                                     Handler handler = new Handler();
  297.                                     handler.postDelayed(new Runnable() {
  298.                                         @Override
  299.                                         public void run() {
  300.                                             b1.setBackgroundColor(Color.parseColor("#FFFFFF"));
  301.                                             b2.setBackgroundColor(Color.parseColor("#FFFFFF"));
  302.                                             b3.setBackgroundColor(Color.parseColor("#FFFFFF"));
  303.                                             b4.setBackgroundColor(Color.parseColor("#FFFFFF"));
  304.                                             updateQuestions();
  305.                                         }
  306.                                     },0);
  307.                                 }
  308.                             }
  309.                         });
  310.                 }
  311.  
  312.                 @Override
  313.                 public void onCancelled(@NonNull DatabaseError databaseError) {
  314.  
  315.                 }
  316.             });
  317.         }
  318.         total++;
  319.     }
  320.  
  321.  
  322.  
  323.     @Override
  324.     protected void onDestroy() {
  325.         super.onDestroy();
  326.         if (countDownTimer != null) {
  327.             countDownTimer.cancel();
  328.         }
  329.     }
  330.  
  331.  
  332. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement