2

I have create 3 number picker, plan to add/ total up those number picker when selected, but now i face some string array problem

input :here the number that i select frm number picker 20, 30 , 10

output: 103020

but i need it to be total up 10+20+30=60 and not show the output like 103020

   NumberPicker.OnValueChangeListener onValueChanged = new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                NumberPicker[] nps = new NumberPicker[3];
                nps[0] = (NumberPicker) findViewById(R.id.carbPercentage);
                nps[1] = (NumberPicker) findViewById(R.id.proteinPercentage);
                nps[2] = (NumberPicker) findViewById(R.id.fatPercentage);

                String temp = "";
                for (int i = 0; i < 3; i++) {
                    String[] values = nps[i].getDisplayedValues();
                    temp = values[nps[i].getValue()] + temp;
                }

                TextView tv = (TextView) findViewById(R.id.totalPercentage);
                tv.setText(temp);
            }
        };

        NumberPicker[] nps = new NumberPicker[3];
        nps[0] = (NumberPicker) findViewById(R.id.carbPercentage);
        nps[1] = (NumberPicker) findViewById(R.id.proteinPercentage);
        nps[2] = (NumberPicker) findViewById(R.id.fatPercentage);

        String[] values = new String[21];
        for (int i = 0; i < values.length; i++) {
            values[i] = Integer.toString(i * 5);
        }

        for (int i = 0; i < 3; i++) {
            nps[i].setMaxValue(values.length - 1);
            nps[i].setMinValue(0);
            nps[i].setDisplayedValues(values);
            nps[i].setOnValueChangedListener(onValueChanged);
        }
Sufian
  • 6,405
  • 16
  • 66
  • 120
Wg Sam
  • 61
  • 1
  • 2
  • 11

3 Answers3

0

I don't understand why you are trying to concatenate the NumberPicker values through string concatenation of 'temp' variable. Instead use this:

Inside onValueChange event handler for the number picker,

long totalSum = 0L;

for(int i=0; i<3; i++)
      totalSum + = nps[i].getValue(); // getValue() method returns int

TextView tv = (TextView) findViewById(R.id.totalPercentage);
tv.setText(String.valueOf(totalSum));

Let me know if this helps.

Adithya Upadhya
  • 2,239
  • 20
  • 28
  • Thank for ur answer @oathkeeper but it add the the step of the number tht i scroll ex: when the 1st number picker scoll 2 time it show 2 , 2nd number scoll 3 time it show 2+3= 5 time , tht i scoll but what i need is the value inside the number picker to be add up ex: 1st np i scoll to 4, 2nd to 1 and 3rd to 5, it will show 4+1+5=10, is the value inside not step so how ? – Wg Sam Dec 01 '15 at 06:42
0

see the below ans of @oathkeeper is true. let me describe one best thing.

"+" it work in different manners.

(String + string) always return string concate.
(String + int/long) always return string concate.
(int/long + string) always return string concate.
(int/long + int/long) always return int/long (Mathamatical opration).

simple and base concept of JAVA.

Vishal Patel
  • 2,931
  • 3
  • 24
  • 60
0

You just need to change nps[i].getDisplayedValues() to nps[i].getValue(), which return a int value. Then add these int values together, then display it.

   NumberPicker.OnValueChangeListener onValueChanged = new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                NumberPicker[] nps = new NumberPicker[3];
                nps[0] = (NumberPicker) findViewById(R.id.carbPercentage);
                nps[1] = (NumberPicker) findViewById(R.id.proteinPercentage);
                nps[2] = (NumberPicker) findViewById(R.id.fatPercentage);

                // change here!
                int sum = 0;
                // String temp = "";
                for (int i = 0; i < 3; i++) {
                    String[] values = nps[i].getDisplayedValues();
                    sum += Integer.parseInt(values[nps[i].getValue()]);
                    // temp = values[nps[i].getValue()] + temp;
                }

                TextView tv = (TextView) findViewById(R.id.totalPercentage);
                tv.setText(String.valueOf(sum));
            }
        };

        NumberPicker[] nps = new NumberPicker[3];
        nps[0] = (NumberPicker) findViewById(R.id.carbPercentage);
        nps[1] = (NumberPicker) findViewById(R.id.proteinPercentage);
        nps[2] = (NumberPicker) findViewById(R.id.fatPercentage);

        String[] values = new String[21];
        for (int i = 0; i < values.length; i++) {
            values[i] = Integer.toString(i * 5);
        }

        for (int i = 0; i < 3; i++) {
            nps[i].setMaxValue(values.length - 1);
            nps[i].setMinValue(0);
            nps[i].setDisplayedValues(values);
            nps[i].setOnValueChangedListener(onValueChanged);
        }
Wesley
  • 4,084
  • 6
  • 37
  • 60
  • Thank for ur answer Wesley but it add the the step of the number tht i scroll ex: when the 1st number picker scoll 2 time it show 2 , 2nd number scoll 3 time it show 2+3= 5 time tht i total scoll but what i need is the value inside the number picker to be add up ex: 1st np i scoll to 4, 2nd to 1 and 3rd to 5, it will show 4+1+5=10, is the value inside not step so how ? – Wg Sam Dec 01 '15 at 06:46
  • @WgSam I've made a few changes, please try again – Wesley Dec 01 '15 at 06:50
  • Thank Wesley solve the problem ! last night i got chg my code same as u show me, but i keep jump out when i scoll , cause i forget to put this String.valueOf(sum) inside my setText , but thank for your help – Wg Sam Dec 01 '15 at 07:14
  • can i get your email address, next time if face somthing can ask for your help ha – Wg Sam Dec 01 '15 at 07:15
  • i facing a problem, please help me take a look here the link http://stackoverflow.com/questions/34085608/passing-number-from-1st-activity-to-the-number-picker?noredirect=1#comment55925191_34085608 – Wg Sam Dec 04 '15 at 11:26