简体   繁体   English

微调项选择-Android Studio

[英]Spinner Item Selection - Android Studio

Hi Guys I am having trouble with a spinner Item selection. 嗨,大家好,我在选择微调器项目时遇到了麻烦。 The concept is a calorie calculator and the current code that I have placed below is the button to calculate the action as well as the spinner listener. 这个概念是卡路里计算器,我在下面放置的当前代码是用于计算动作以及旋转监听器的按钮。 I have tested the users input which is working fine as well as testing what the spinner string is equaling, however it is not setting my if statement to true. 我已经测试了用户输入是否正常,还测试了微调字符串等于什么,但是它没有将我的if语句设置为true。 Can anyone enlighten me on what is the issue? 有人可以启发我解决什么问题吗?

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


        }
    }); 

` `

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

(itemSelectedDiscount.equals("STEAK"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM