简体   繁体   中英

How to get the value of Seekbar for calculation?

Above the generateData() method, i declared an integer variable bw, i want it to be equal to the value of the current seekbar progress, how to do that? or actually can i directly use the value of progress for calculation?? Sorry it may sound a stupid question but i am absolute new to programming.

If I put bw=progress; inside the onprogresschanged, the outcome of bw*10 and bw*9 will become 0 no matter how i slide the seekbar.

public class MainActivity extends AppCompatActivity {
int bw;

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

        MyAdapter adapter = new MyAdapter(this, generateData());
        ListView listView = (ListView) findViewById(R.id.listview);
        listView.setAdapter(adapter);

        SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar);
        final TextView seekBarValue = (TextView) findViewById(R.id.valuetextview);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                bw=progress;
                seekBarValue.setText(String.valueOf(progress));

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }

        });

    }



    //Detail of drugs in following formatL "Drug Name", doseperkg,"frequency"
    private ArrayList<DrugItem> generateData() {
        ArrayList<DrugItem> items = new ArrayList<DrugItem>();
        items.add(new DrugItem("Ceclor", bw * 10, "mg every   8 hours"));
        items.add(new DrugItem("Cedax", bw * 9, "mg every 24 hours"));
        items.add(new DrugItem("Zinnat", bw * 10, "mg every 12 hours"));
        items.add(new DrugItem("Zithromax", bw * 10, "mg every 24 hours"));


        return items;
    }
}

yes obvious.

In below method int progress this is your main point value. U can use it for calculation .

but make sure work around in onProgressChanged method.

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// your calculation...


            }

in the callback methods of seekbar

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                bw=progress;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }

        });

in addition to that you may want to set a maximum value to seekbar's seek

seekBar.setMax(max_value-min_value);

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