pabloducato

QuizOnlineActivity

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