微调项目选择 - Android Studio

时间:2017-04-21 08:23:36

标签: java android spinner

嗨大家我在使用微调器选项时遇到问题。这个概念是一个卡路里计算器,我在下面放置的当前代码是计算动作的按钮以及微调器监听器。我测试了用户输入正常工作以及测试微调器字符串相等的内容,但是它没有将我的if语句设置为true。任何人都可以告诉我这是什么问题吗?

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


        }
    }); 

`

1 个答案:

答案 0 :(得分:0)

我没有看到所有代码,但我认为您应该使用if条件string.equals(Object other)而不是“==”运算符。

(itemSelectedDiscount.equals("STEAK"))