0

I have create the 3 different number picker, I'm using the 3 different number selected to do calculation, but don't know why it wont show the total sum in the totalP textView. Is that this 2 line is wrong ? "sum =(a*4)+(b*4)+(c*9); " and "totalP.setText(sum+" cal");"

    public TextView carb;
    public TextView protein;
    public TextView fat;
    public TextView totalP;
    int carbNum,proteinNum,fatNum,cpv,ppv,fpv;
    int sum,a,b,c;

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

        Bundle extras = getIntent().getExtras();

        carb = (TextView) findViewById(R.id.carbsNum);
        carbNum = extras.getInt("Carb");
        carb.setText(carbNum+" g");

        protein = (TextView) findViewById(R.id.proteinNum);
        proteinNum = extras.getInt("Protein");
        protein.setText(proteinNum+" g");

        fat = (TextView) findViewById(R.id.fatNum);
        fatNum = extras.getInt("Fat");
        fat.setText(fatNum+" g");

        cpv=extras.getInt("carbP");
        ppv=extras.getInt("proteinP");
        fpv=extras.getInt("fatP");

        totalP=(TextView) findViewById(R.id.totalPercentage);

        final NumberPicker np1 = (NumberPicker) findViewById(R.id.carbPercentage);
        final NumberPicker np2 = (NumberPicker) findViewById(R.id.proteinPercentage);
        final NumberPicker np3 = (NumberPicker) findViewById(R.id.fatPercentage);

        np1.setMaxValue(1000);
        np1.setMinValue(0);
        np1.setValue(carbNum);
        np1.setWrapSelectorWheel(true);
        np1.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                a =np1.getValue();;
                carb.setText(a + " g");
            }
        });
        np2.setMaxValue(1000);
        np2.setMinValue(0);
        np2.setValue(proteinNum);
        np2.setWrapSelectorWheel(true);
        np2.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                b = np2.getValue();
                protein.setText(b + " g");

            }
        });
        np3.setMaxValue(1000);
        np3.setMinValue(0);
        np3.setValue(fatNum);
        np3.setWrapSelectorWheel(true);
        np3.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                c = np3.getValue();
                fat.setText(c + " g");
            }
        });

        sum =(a*4)+(b*4)+(c*9);
        // this line cant work, is because the a,b,c ??
        totalP.setText(sum+" cal");  
        // this line cant work  ??
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

    }
}
Wg Sam
  • 61
  • 1
  • 2
  • 11
  • u have to make ur methods return values of a, b, and c then you get them together with sum – JBALI Dec 05 '15 at 16:04

1 Answers1

0

put that two line in all change listener, it will work, cureently in oncreate function a,b,c has no value until setOnValueChangedListener execute, thats why that 2 line is not working

Kishor datta gupta
  • 1,103
  • 2
  • 14
  • 42
  • I have copy tht 2 line in all change listener, it work, but when i select the 1st number picker it only show the calculate the 1st one the 2nd and 3rd it ignore , how can i solve it, i want it to add with other 2, it is i want to change it to array ? – Wg Sam Dec 06 '15 at 03:50