简体   繁体   中英

How do I pass a variable into a Runnable?

In MainActivity I have a Runnable that I want to pass variable to. I tried to add a TextView timerValue in the Runnable() in my second example, but that's not the right way.

private Runnable updateTimerThread = new Runnable() {

    public void run() {

        timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

        updatedTime = timeSwapBuff + timeInMilliseconds;

        int secs = (int) (timeInMilliseconds / 1000);
        int mins = secs / 60;
        secs = secs % 60;
        int hours = mins / 60;
        mins = mins % 60;
        recordtimer = "Recording Time: " + String.format("%02d", hours) + ":" + String.format("%02d", mins) + ":" + String.format("%02d", secs);
        timerValueRecord.post(new Runnable()
        {
            public void run()
            {
                timerValueRecord.setText(recordtimer);
            }
        });
                //set yout textview to the String timer here
        customHandler.postDelayed(this, 1000);
    }

};

I need it to get TextView so instead of using the timerValueRecord it uses, for example, the TextView timerValue .

The logic is something like this:

private Runnable updateTimerThread = new Runnable(TextView timerValue){

    public void run() {

        timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

        updatedTime = timeSwapBuff + timeInMilliseconds;

        int secs = (int) (timeInMilliseconds / 1000);
        int mins = secs / 60;
        secs = secs % 60;
        int hours = mins / 60;
        mins = mins % 60;
        recordtimer = "Recording Time: " + String.format("%02d", hours) + ":" + String.format("%02d", mins) + ":" + String.format("%02d", secs);
        timerValue.post(new Runnable()
        {
            public void run()
            {
                timerValue.setText(recordtimer);
            }
        });
                //set yout textview to the String timer here
        customHandler.postDelayed(this, 1000);
    }

};

Create a class that implements Runnable and have the constructor of your class take the inputs, or provide setters to set the values. One way to do it is:

public class UpdateTimerThread implements Runnable {
    protected TextView timerValue;

    public UpdateTimerThread(TextView timerValue) {
        this.timerValue= timerValue;
    }

    public void run() {
       ....
       // you can access timerValue here
    }
}

private Runnable updateTimerThread = new UpdateTimerThread(timerValue);

Creating another class (as suggested) works well. Or, you could use an anonymous inner class. Such a class has access to any final variables that enclose it. Eg,

public void test() {
    final TextView timerValue = ... set to whatever;

    Runnable updateTimerThread = new Runnable() {

        public void run() {

            // ... blah blah

            timerValue.doSomething  // <== access to final variable OK

        }
    };
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM