I have two number picker eg: reading numberpicker and poem numberpicker, each student is getting 10 points by default, every time a student makes a mistake the number picker increases and points should decrease by say (0.5) points, example code given below, please help how to achieve this particular method. Both number picker have increment and decrement method, both number pickers should add or subtract the same textView which default value is 10.
public class MainActivity extends AppCompatActivity {
private NumberPicker readingPicker;
private NumberPicker poempicker;
private TextView pointsTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
readingPicker = findViewById(R.id.reading_picker);
poempicker = findViewById(R.id.poem_picker);
// increase or decrease value of points text view (10 default) using below number pickers.
pointsTextView = findViewById(R.id.ten_points);
readingPicker.setValueChangedListener(new ValueChangedListener() {
@Override
public void valueChanged(int value, ActionEnum action) {
switch (action){
case INCREMENT:
// TODO: decrease value of pointsTextView by 0.5
break;
case DECREMENT:
// TODO: increase value of pointsTextView by 0.5
}
}
});
// after getting the value from above reading picker
poempicker.setValueChangedListener(new ValueChangedListener() {
@Override
public void valueChanged(int value, ActionEnum action) {
switch (action) {
case INCREMENT:
// TODO: decrease value of same pointsTextView by 0.5
break;
case DECREMENT:
// TODO: increase value of same pointsTextView by 0.5
}
}
});
}
}
I was able to subtract using the reading number picker, but after that, I am little confused on how to get the current value of the textview and further subtract using the second number picker.
readingPicker.setValueChangedListener(new ValueChangedListener() {
@Override
public void valueChanged(int value, ActionEnum action) {
switch (action) {
case INCREMENT:
double currentValue = Double.parseDouble(pointsTextView.getText().toString());
currentValue = currentValue + 0.5;
pointsTextView.setText(String.valueOf(currentValue));
break;
case DECREMENT:
double currentValue1 = Double.parseDouble(pointsTextView.getText().toString());
currentValue1 = currentValue1 - 0.5;
pointsTextView.setText(String.valueOf(currentValue1));
}
}
});
This way you can achieve the desired result. Getting current value of pointsTextView
and then doing add or subtract operation on it and again, setting that value to the pointsTextView
.