[英]Android Studio Select item from SQLite database by spinner selection
[英]Spinner Item Selection - Android Studio
嗨,大家好,我在選擇微調器項目時遇到了麻煩。 這個概念是卡路里計算器,我在下面放置的當前代碼是用於計算動作以及旋轉監聽器的按鈕。 我已經測試了用戶輸入是否正常,還測試了微調字符串等於什么,但是它沒有將我的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);
}
}
});
`
我看不到所有代碼,但我想您應該在if條件中使用string.equals(Object other)而不是“ ==”運算符。
(itemSelectedDiscount.equals("STEAK"))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.