gojic
gojic

Reputation: 363

CountDownTimer won't be reset

I am creating a simple app, where the user will test his math.The result of two numbers is displeyed in one of four button.User is guessing result and adding to score.He has 30seconds to do as many as possible correct answers.After 30 seconds score will pop up with play again button.The problem is that if he press play again button ,timer won't reset ??

my code:

    public class MainActivity extends AppCompatActivity {

    private Button dugme1;
    private Button dugme2;
    private Button dugme3;
    private Button dugme4;
    private Button startDugme;
    private List<Integer> list = new ArrayList<>();
    private int lokacijaTacnogOdgovora;
    private TextView zadatak;
    private TextView krajnjiRezultat;
    private TextView tajmer;
    private int vrednostKliknuyogPolja;
    private int tacanOdgovor;
    private int brojPokusaja;
    private int brojPogodnjenihOdgovora;
    private Random random = new Random();
     CountDownTimer countDownTimer;
    private long preostaloVreme = 30000;
    private LinearLayout linearLayout;
    private TextView rezultat;
    Button dugmeZaPonovi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //pronadjeni odgovarajuci Button
        dugme1 = findViewById(R.id.dugme);
        dugme2 = findViewById(R.id.dugme2);
        dugme3 = findViewById(R.id.dugme3);
        dugme4 = findViewById(R.id.dugme4);
        startDugme = findViewById(R.id.start_dugme);
        linearLayout = findViewById(R.id.linerni_lejaut);
        dugmeZaPonovi = findViewById(R.id.dugme_za_nastavak);

        //Pronadjeni odgovarujci TextView
        zadatak = findViewById(R.id.sabiranje_text_view);
        krajnjiRezultat = findViewById(R.id.krajnji_rezultat);
        tajmer = findViewById(R.id.tajmer_text_view);
        rezultat = findViewById(R.id.rezultat);
        linearLayout.setVisibility(View.INVISIBLE);
        linearLayout.setBackgroundColor(0xFF00FF00);
        linearLayout.setPadding(10,10,10,10);

        ispisivanjeDugmeta();

    }

    private void startovanjeTajmera() {
        countDownTimer = new CountDownTimer(preostaloVreme, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                preostaloVreme = millisUntilFinished;
                updateTajmer();
            }

            @Override
            public void onFinish() {
                linearLayout.setVisibility(View.VISIBLE);
                rezultat.setText(brojPogodnjenihOdgovora + " / " + brojPokusaja);
                startDugme(findViewById(R.id.start_dugme));
            }
        }.start();
    }

    public void ispisivanjeDugmeta() {
        //saboranje dva broja i prikazivanje rezultata u odgovarajuci textview
        int prviBroj = random.nextInt(21);
        int drugiBroj = random.nextInt(21);
        tacanOdgovor = prviBroj + drugiBroj;
        zadatak.setText(prviBroj + " + " + drugiBroj);
        krajnjiRezultat.setText(brojPogodnjenihOdgovora + " / " + brojPokusaja);
        //prikazivanje random brojeva i postavljanje tacnog rezultata na odgovarujce mesto
        lokacijaTacnogOdgovora = random.nextInt(4);
        int netacanOdgovor = 0;
        for (int i = 0; i < 4; i++) {
            if (i == lokacijaTacnogOdgovora) {
                list.add(tacanOdgovor);

            } else {
                netacanOdgovor = random.nextInt(41);
                while (netacanOdgovor == (tacanOdgovor)) {
                    netacanOdgovor = random.nextInt(41);
                }
                list.add(netacanOdgovor);
            }
        }
        //prikazivanje nekih vrednosti ali i tacnog rezultata na dugmad
        dugme1.setText(Integer.toString(list.get(0)));
        dugme2.setText(Integer.toString(list.get(1)));
        dugme3.setText(Integer.toString(list.get(2)));
        dugme4.setText(Integer.toString(list.get(3)));

        list.clear();
        updateTajmer();
    }

    public void kliknuto(View view) {

        Button btn = (Button) view;
        String kliknutoPolje = " ";
        kliknutoPolje = btn.getText().toString();
        vrednostKliknuyogPolja = Integer.parseInt(kliknutoPolje);

        if (vrednostKliknuyogPolja == tacanOdgovor) {
            brojPogodnjenihOdgovora++;
            brojPokusaja++;
            krajnjiRezultat.setText(brojPogodnjenihOdgovora + " / " + brojPokusaja);
        } else {
            brojPokusaja++;
            krajnjiRezultat.setText(brojPogodnjenihOdgovora + " / " + brojPokusaja);
        }
        ispisivanjeDugmeta();
    }

    public void startDugme(View view) {
        startovanjeTajmera();
        startDugme.setVisibility(View.INVISIBLE);
    }

    private void updateTajmer() {
        int sekunde = (int) preostaloVreme % 60000 / 1000;
        String preostaloVreme = "";

        if (sekunde < 10) {
            preostaloVreme += "0";
        }
        preostaloVreme += "" + sekunde;
        tajmer.setText(preostaloVreme + " s");
    }

    public void igrajPonovo(View view) {
        brojPokusaja = 0;
        brojPogodnjenihOdgovora = 0;
startovanjeTajmera();
        krajnjiRezultat.setText(brojPogodnjenihOdgovora + " / " + brojPokusaja);
        linearLayout.setVisibility(View.INVISIBLE);

    }
}

Upvotes: 0

Views: 61

Answers (1)

Kubik
Kubik

Reputation: 610

You are not resetting preostaloVreme property back to 30000 when you want to start new timer. Change your startovanjeTajmera function to something like this:

private void startovanjeTajmera() {
        preostaloVreme = 30000;
        countDownTimer = new CountDownTimer(preostaloVreme, 1000) {
     ...
}

Upvotes: 1

Related Questions