Advertisement
pabloducato

handler

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