Question

Good afternoon,

I am looking to assign a generic variable that can be changed throughout the app and passed into CountDownTimer as my long

package com.rcd.learningactivities;

import java.text.DecimalFormat;
import java.util.concurrent.TimeUnit;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    protected CountDownTimer cd;
    private long countTime = 0;  // Variable for CountDownTimer
    private Button lastPressedButton;
    private Button red;
    private Button blue;
    private TextView blueTimer;
    private TextView redTimer;


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


        blueTimer = (TextView) findViewById(R.id.blueTimer); 
        blue = (Button) findViewById(R.id.button1);

        redTimer = (TextView) findViewById(R.id.redTimer); 
        red = (Button) findViewById(R.id.button2);


        cd = new CountDownTimer(countTime, 1000) {  //<--- trying to use countTime here

             public void onTick(long millisUntilFinished) {
                    DecimalFormat dfmin = new DecimalFormat("0");
                    DecimalFormat dfsec = new DecimalFormat("00");
                    double seconds = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished)%60;
                    double min = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished);
                    if (lastPressedButton == blue){
                        blueTimer.setText(String.valueOf(dfmin.format(min)) + ":" + String.valueOf(dfsec.format(seconds)));
                    }
                    else if (lastPressedButton == red) {
                        redTimer.setText(String.valueOf(dfmin.format(min)) + ":" + String.valueOf(dfsec.format(seconds)));
                    }

             }

             public void onFinish() {
                    if (lastPressedButton == blue){
                        blueTimer.setText("5:00");
                    }
                    else if (lastPressedButton == red){
                        redTimer.setText("5:00");
                    }
                }
          };

        blue.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                lastPressedButton = blue;
                countTime = 300000; // <-- setting countTime here
                if (blueTimer.getText().toString().contains("5:00")){
                    cd.start();

                } else {
                    cd.cancel();
                    cd.onFinish();
                }

            }
        });

        red.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                lastPressedButton = red;
                countTime = 250000;
                if (redTimer.getText().toString().contains("5:00")){
                    cd.start();
                } else {
                    cd.cancel();
                    cd.onFinish();
                }

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

For whatever reason, it stays with the initially declared number 0 (or if i change it to say 300000 then its always 300000) and its not changing the countTime below in my onClick.

Any help is greatly appreciated. If it can be accompanied by an explanation, that would be great!

Thanks everyone in advance!

EDIT: Im guessing its a scope issue that im overlooking??

EDIT2: If it is a scope issue, im a little confused as to how im able to reset the "lastPressedButton" variable set just about it, but not the countTime.

Was it helpful?

Solution

Java has pass by value semantics, not pass by reference, the value of countTime at construction is passed to CountDownTimer, CDT does not see any updates to countTime after it has been instantiated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top