D
D
Denis2017-05-14 11:08:56
Java
Denis, 2017-05-14 11:08:56

How to get long variable from NumberPicker?

I'm new to programming. There is a task to write a timer with two readings (Tabata timer). I want to set the countdown time using NumberPicker. I can't figure out how to get a long type variable from NumberPicker?
Here is the code:

package com.example.fitness_application;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.NumberPicker;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * Created by Денис on 28.02.2017.
 */
public class ActivityTimer extends AppCompatActivity {

    private TextView mTextField;
    private NumberPicker PickerSecWork;
    private NumberPicker PickerSecRest;
    long secWork;
    long secRest;
    boolean mIsRunning = false;
    int mCurrentPeriod = 0;

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

        mTextField = (TextView) findViewById(R.id.textView10);
        PickerSecWork = (NumberPicker) findViewById(R.id.PickerWO);
        PickerSecRest = (NumberPicker) findViewById(R.id.PickerR);
        PickerSecWork.setMinValue(0);
        PickerSecWork.setMaxValue(59);
        PickerSecRest.setMinValue(0);
        PickerSecRest.setMaxValue(59);
        PickerSecWork.setOnValueChangedListener(onValueChanged);
    }

    public void TimerWorkOut() {
        new CountDownTimer(secWork, 1000) {

            public void onTick(long millisUntilFinished) {
                mTextField.setText(" " + millisUntilFinished / 1000);
            }

            public void onFinish() {
                mTextField.setText("Rest!");
            }
        }.start();
    }

    public void TimerRest() {
        new CountDownTimer(secRest, 1000) {

            public void onTick(long millisUntilFinished) {
                mTextField.setText(" " + millisUntilFinished / 1000);
            }

            public void onFinish() {
                mTextField.setText("Just do it!");
            }
        }.start();
    }

    public void ClickStart(View view) {
        TimerWorkOut();
        if(secWork == 0) {
            TimerRest();
        }
    }

    public void ClickCancel(View v) {

    }

    NumberPicker.OnValueChangeListener onValueChanged = new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            if (!mIsRunning) {
                mTextField.setText(intToTime(newVal));
                mCurrentPeriod = newVal;
            }
            int value = PickerSecWork.getValue();
            secWork = Integer.parseInt(String.valueOf(value));
        }
    };

    private String intToTime(int i) {
        return (new SimpleDateFormat("ss")).format(new Date(i * 1000));
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Bayov, 2017-05-14
@Manitu_PC

Maybe I misunderstood the question. Is a variable of type long required?
Well, in this place

int value = PickerSecWork.getValue();
secWork = Integer.parseInt(String.valueOf(value));

You first get an int value from PickerSecWork, then you convert it to a string, and again you parse an int from the string.
If you need long, then...
//Расширяющее преобразование. Можно даже без cast
secWork = PickerSecWork.getValue();

Or
int value = PickerSecWork.getValue();
secWork = Long.parseLong(String.valueOf(value));
//или secWork = Long.valueOf(String.valueOf(value));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question