2015-12-05 56 views
0

我已經創建了3個不同的數字選擇器,我使用3個不同的數字來進行計算,但不知道爲什麼它不會顯示totalP textView中的總數。這2條線是錯誤的嗎? 「sum =(a * 4)+(b * 4)+(c * 9);」和「totalP.setText(sum +」cal「);」Number Picker calculaiton

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); 

    } 
} 
+0

u有使ur方法返回a,b和c的值,然後你得到他們在一起總和 – JBALI

回答

0

把那兩個線路中的所有變化監聽器,它會工作, cureently在OnCreate中功能A,B,C有沒有價值,直到setOnValueChangedListener執行,這就是爲什麼這2線無法正常工作

+0

可以顯示代碼,即時通訊混淆 –

+0

我已經複製所有更改監聽器中的2行,它的工作,但是當我選擇1st數字選擇器它只顯示計算第一個第二和第三個它忽略,我如何解決它,我想要它與其他2添加,它是我想要將其更改爲數組? –