2017-04-21 69 views
0

嗨,大家好我有一個微調項目選擇的麻煩。這個概念是一個卡路里計算器,我下面放置的當前代碼是計算動作的按鈕以及spinner監聽器。我測試了用戶輸入工作正常,以及測試微調字符串是什麼,但它沒有設置我的if語句爲真。任何人都可以啓發我什麼是問題?微調項目選擇 - Android Studio

enum Discount { 
    STEAK(2.71f), 
    CHICKEN(2.39f), 
    PORK(2.42f), 
    HAM(1.45f), 
    VEAL(1.72f), 
    WHITEFISH(1.72f), 
    SALMON(2.08f); 

    private float amount; 
    Discount(float amount) { 
     this.amount = amount; 
    } 


} 




dropDownList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
       itemSelectedDiscount = dropDownList.getSelectedItem().toString(); 
      } 

     @Override 
     public void onNothingSelected(AdapterView<?> parent) { 

     } 
    }); 



    Button calculate = (Button)findViewById(R.id.calculate); 
    calculate.setOnClickListener(new View.OnClickListener(){ 
     public void onClick(View v){ 
      totalCaloriesInt = value; 
      status.setText(itemSelectedDiscount); 
      if (itemSelectedDiscount == "STEAK") { 
      Discount steak = Discount.STEAK; 
       calculatedCalories.setText("" + totalCaloriesInt * steak.amount); 
      } 

      else if (itemSelectedDiscount == "CHICKEN") { 
       Discount chicken = Discount.CHICKEN; 
       calculatedCalories.setText("" + totalCaloriesInt * chicken.amount); 
      } 


     } 
    }); 

`

回答

0

我沒有看到所有的代碼,但我想你應該在你如果不是「==」操作符條件string.equals(Object other)使用。

(itemSelectedDiscount.equals("STEAK")) 
+0

WOW!感謝<3這個簡單的錯誤 –