Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.quiz.AccountActivity;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Color;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import android.os.Bundle;
- import android.os.CountDownTimer;
- import android.os.Handler;
- import android.support.annotation.NonNull;
- import android.support.annotation.Nullable;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.example.quiz.AccountActivity.Model.QuestionFirebase;
- import com.example.quiz.R;
- import com.google.firebase.database.DataSnapshot;
- import com.google.firebase.database.DatabaseError;
- import com.google.firebase.database.DatabaseReference;
- import com.google.firebase.database.FirebaseDatabase;
- import com.google.firebase.database.ValueEventListener;
- public class QuizOnlineActivity extends AppCompatActivity {
- Button beback,b1,b2,b3,b4,accept,reset;
- TextView question, timer, maintitle;
- private CountDownTimer countDownTimer;
- int total = 1;
- int questiontag = 0;
- int correct = 0;
- int wrong = 0;
- DatabaseReference databaseReference;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_quiz_online);
- b1 = findViewById(R.id.button1);
- b2 = findViewById(R.id.button2);
- b3 = findViewById(R.id.button3);
- b4 = findViewById(R.id.button4);
- question = findViewById(R.id.question);
- timer = findViewById(R.id.timer);
- maintitle = findViewById(R.id.maintitle);
- accept = findViewById(R.id.accept);
- reset = findViewById(R.id.reset);
- beback = findViewById(R.id.beback);
- beback.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent(QuizOnlineActivity.this, ProfileActivity.class);
- startActivity(intent);
- }
- });
- accept.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- maintitle.setVisibility(v.GONE);
- accept.setText("Potwierdź");
- beback.setText("Przerwij");
- b1.setVisibility(v.VISIBLE);
- b2.setVisibility(v.VISIBLE);
- b3.setVisibility(v.VISIBLE);
- b4.setVisibility(v.VISIBLE);
- question.setVisibility(v.VISIBLE);
- timer.setVisibility(v.VISIBLE);
- reset.setVisibility(v.VISIBLE);
- updateQuestions();
- }
- });
- }
- private boolean isNetworkAvailable() {
- ConnectivityManager connectivityManager
- = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
- return activeNetworkInfo != null && activeNetworkInfo.isConnected();
- }
- private void updateQuestions()
- {
- if(isNetworkAvailable()==false)
- {
- Toast.makeText(QuizOnlineActivity.this, "Brak połączenia z internetem", Toast.LENGTH_SHORT).show();
- }
- else if(isNetworkAvailable()==true && total==31)
- {
- Intent i = new Intent(getApplicationContext(), ResultActivity.class);
- i.putExtra("Łącznie pytań:", String.valueOf(questiontag));
- i.putExtra("Poprawne odpowiedzi:", String.valueOf(correct));
- i.putExtra("Złe odpowiedzi:", String.valueOf(wrong));
- startActivity(i);
- }
- else
- {
- databaseReference = FirebaseDatabase.getInstance().getReference().child("questions").child(String.valueOf(total));
- databaseReference.addValueEventListener(new ValueEventListener() {
- @Override
- public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
- final QuestionFirebase questionFirebase = dataSnapshot.getValue(QuestionFirebase.class);
- question.setText(questionFirebase.getQuestion());
- b1.setText(questionFirebase.getOption1());
- b2.setText(questionFirebase.getOption2());
- b3.setText(questionFirebase.getOption3());
- b4.setText(questionFirebase.getOption4());
- //final boolean[] tmp1 = new boolean[1];
- //final boolean[] tmp2 = new boolean[1];
- //final boolean[] tmp3 = new boolean[1];
- //final boolean[] tmp4 = new boolean[1];
- //tmp1[0]=true;
- //tmp2[0]=true;
- //tmp3[0]=true;
- //tmp4[0]=true;
- //final String[] s1 = {String.valueOf(tmp1)};
- //final String[] s2 = {String.valueOf(tmp2)};
- //final String[] s3 = {String.valueOf(tmp4)};
- //final String[] s4 = {String.valueOf(tmp4)};
- final boolean[] varControlOne = {true};
- final boolean[] varControlTwo = {true};
- final boolean[] varControlThree = {true};
- final boolean[] varControlFour = {true};
- if(b1.getText().toString().equals(questionFirebase.getAnswer1()))
- {
- varControlOne[0] = false;
- }
- else
- {
- varControlOne[0] = true;
- }
- if(b2.getText().toString().equals(questionFirebase.getAnswer2()))
- {
- varControlTwo[0] = false;
- }
- else
- {
- varControlTwo[0] = true;
- }
- if(b3.getText().toString().equals(questionFirebase.getAnswer3()))
- {
- varControlThree[0] = false;
- }
- else
- {
- varControlThree[0] = true;
- }
- if(b4.getText().toString().equals(questionFirebase.getAnswer4()))
- {
- varControlFour[0] = false;
- }
- else
- {
- varControlFour[0] = true;
- }
- b1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- b1.setBackgroundColor(Color.YELLOW);
- if(b1.getText().toString().equals(questionFirebase.getAnswer1()))
- {
- varControlOne[0] = true;
- }
- }
- });
- b2.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- b2.setBackgroundColor(Color.YELLOW);
- if(b2.getText().toString().equals(questionFirebase.getAnswer2()))
- {
- varControlTwo[0] = true;
- }
- }
- });
- b3.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- b3.setBackgroundColor(Color.YELLOW);
- if(b3.getText().toString().equals(questionFirebase.getAnswer3()))
- {
- varControlThree[0] = true;
- }
- }
- });
- b4.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- b4.setBackgroundColor(Color.YELLOW);
- if(b4.getText().toString().equals(questionFirebase.getAnswer4()))
- {
- varControlFour[0] = true;
- }
- }
- });
- reset.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- b1.setBackgroundColor(Color.WHITE);
- b2.setBackgroundColor(Color.WHITE);
- b3.setBackgroundColor(Color.WHITE);
- b4.setBackgroundColor(Color.WHITE);
- varControlOne[0] = true;
- varControlTwo[0] = true;
- varControlThree[0] = true;
- varControlFour[0] = true;
- if(b1.getText().toString().equals(questionFirebase.getAnswer1()))
- {
- varControlOne[0] = false;
- }
- else
- {
- varControlOne[0] = true;
- }
- if(b2.getText().toString().equals(questionFirebase.getAnswer2()))
- {
- varControlTwo[0] = false;
- }
- else
- {
- varControlTwo[0] = true;
- }
- if(b3.getText().toString().equals(questionFirebase.getAnswer3()))
- {
- varControlThree[0] = false;
- }
- else
- {
- varControlThree[0] = true;
- }
- if(b4.getText().toString().equals(questionFirebase.getAnswer4()))
- {
- varControlFour[0] = false;
- }
- else
- {
- varControlFour[0] = true;
- }
- }
- });
- accept.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if(varControlOne[0] == true && varControlTwo[0] == true && varControlThree[0] == true && varControlFour[0]==true)
- {
- correct++;
- questiontag++;
- Handler handler = new Handler();
- handler.postDelayed(new Runnable() {
- @Override
- public void run() {
- //countDownTimer.cancel();
- b1.setBackgroundColor(Color.parseColor("#FFFFFF"));
- b2.setBackgroundColor(Color.parseColor("#FFFFFF"));
- b3.setBackgroundColor(Color.parseColor("#FFFFFF"));
- b4.setBackgroundColor(Color.parseColor("#FFFFFF"));
- updateQuestions();
- }
- },0);
- }
- else
- {
- wrong++;
- questiontag++;
- Handler handler = new Handler();
- handler.postDelayed(new Runnable() {
- @Override
- public void run() {
- b1.setBackgroundColor(Color.parseColor("#FFFFFF"));
- b2.setBackgroundColor(Color.parseColor("#FFFFFF"));
- b3.setBackgroundColor(Color.parseColor("#FFFFFF"));
- b4.setBackgroundColor(Color.parseColor("#FFFFFF"));
- updateQuestions();
- }
- },0);
- }
- }
- });
- }
- @Override
- public void onCancelled(@NonNull DatabaseError databaseError) {
- }
- });
- }
- total++;
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- if (countDownTimer != null) {
- countDownTimer.cancel();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement