简体   繁体   中英

Android Seekbar progress changed variable giving errors

On several examples I found, including the links below, it is recommended to use an integer to track the progress change in order to stuff it into a TextView --so that the user get's the current number of the seekbar.

http://examples.javacodegeeks.com/android/core/widget/seekbar/android-seekbar-example/

http://javatechig.com/android/android-seekbar-example

However, when I try setting the progressChanged variable to final, I get an error message, if I don't set it to final I get an error message (see comment remarks in code below). I looked in Stack Overflow for an explanation but could not find one, so any help is appreciated. On a similar note, in the examples in the link (and below if it worked), how does Android know that the variable progressChanged should be applied to the textView?

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


    textViewUrgent.setText("Urgency: " + seekBarUrgent.getProgress()+" of " + seekBarUrgent.getMax());

    //if I change this to final int progressChanged I get an error message...
    //...below that the variable 'Cannot assign a value to final variable progressChanged'
    final int progressChanged = 0;

    seekBarUrgent.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        //If I don't set progressChanged variable to final, I get an error message telling me to do so
        progressChanged = progress;
            Toast.makeText(getApplicationContext(), "Changing seekbar's progress", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            Toast.makeText(getApplicationContext(), "Started tracking seekbar", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            Toast.makeText(getApplicationContext(), "Stopped tracking seekbar", Toast.LENGTH_SHORT).show();
        }
    });

}

Declare progressChanged variable as global. You will not be forced to declare it as global.

When you declare the variable inside onCreate() and use it in another class(in this case listener), compiler will force you to declare the variable as final, so that, the context where the variable is declared can not modify the value of it.

Okay, so I got it to work, and I think it's pretty straight forward. No global variables required. In fact, no variables required. And I still connot figure out how the code in the links in my original post were supposed to update the text to the current seekbar value. Anyhow, I just repeated the line (where I set the text values) textViewUrgent.setText("Urgency... in public void onProgressChanged(... Unless I'm missing something, wouldn't this be the obvious approach for updating the textView to match the seekbar's current progress?

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

    // Initialize the textview with '0'.
    textViewUrgent.setText("Urgency: " + seekBarUrgent.getProgress() + " of " + seekBarUrgent.getMax());

    seekBarUrgent.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            textViewUrgent.setText("Urgency: " + seekBarUrgent.getProgress() + " of " + seekBarUrgent.getMax());
            Toast.makeText(getApplicationContext(), "Changing seekbar's progress", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            Toast.makeText(getApplicationContext(), "Started tracking seekbar", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            Toast.makeText(getApplicationContext(), "Stopped tracking seekbar", Toast.LENGTH_SHORT).show();
        }
    });

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