Frage

Background: I have one activity, with a Textview and a button. Clicking the button runs SetValue() which opens a dialog that contains a Seekbar, which in turn sets the value for the textview. Here's the code:

    public void SetValue(View view){
    ShowDialog();
    }


    public void ShowDialog() {

    final AlertDialog.Builder popDialog = new AlertDialog.Builder(this);
    final SeekBar seek = new SeekBar(this);
    int value = 0;
    try {
        value = Integer.parseInt(tv.getText().toString());
    } catch(NumberFormatException nfe) {
       System.out.println("Could not parse " + nfe);
    } 

    seek.setProgress(value);
    seek.setMax(100);
    popDialog.setView(seek);
    seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
    //Update textview value
    tv.setText("Value : " + progress);
    }
        public void onStartTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub
        }
        public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
        }
    });
    // Button 
    popDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
            }
            });
    popDialog.create();
    popDialog.show();
    }

So it works fine, displaying the initial textview progress, and lets me update it with the seekbar.

Problem: After initially setting the value, if I try to use the method again to change the text, the progress on the seekbar starts at 0 every time, when it should be at the previously selected value/current textview value.

Also, I can set a max value for the bar, can I set a min value? Ie -100? How do I set a range for the seekbar?

Thanks for all advice!

Update 1: I initialized the variable at the start of the class, so that eliminated the int value = 0; line, but the problem persists - the seekbar always opens with the progress at the original text value of the textview.

War es hilfreich?

Lösung

First of all, you need to persist your value when the user finishes 'seeking' the SeekBar. We do this in onStopTrackingTouch:

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
    //Update textview value
    tv.setText("Value : " + progress);
}
public void onStartTrackingTouch(SeekBar arg0) {
    // TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
    // Update your preferences only when the user has finished moving the seek bar
    SharedPreferences prefs = getContext().getSharedPreferences("mySharedPrefsFilename", Context.MODE_PRIVATE);
    // Don't forget to call commit() when changing preferences.
    prefs.edit().putInt("seekBarValue", seekBar.getProgress()).commit();
}

Now you want to display this persisted value whenever the dialog is created - so you'll need to retrieve it. You can do this like this:

int value = 0;
SharedPreferences prefs = getContext().getSharedPreferences("mySharedPrefsFilename", Context.MODE_PRIVATE);
value = prefs.getInt("seekBarValue", 0); // 0 is default

seek.setProgress(value);

// You also need to update your TextView to show the saved value
tv.setText("Value : " + value);

You can wrap the access to the SharedPreferences in a class; this is one way to ensure you're using the same preferences file name ("mySharedPrefsFilename" in this example) and the same preference keys to store/retrieve the value ("seekbarValue" in this example).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top