gpt4 book ai didi

java - android studio 中旋转器的确切数据类型是什么?

转载 作者:行者123 更新时间:2023-12-01 19:01:02 25 4
gpt4 key购买 nike

我想在 Android Studio 中编写一个抵押贷款计算器应用程序。我使用旋转器来选择抵押贷款的长度。我想使用所选项目的值进行一些计算。我想做一个语句来将所选项目的值与字符串变量进行比较,但 if 语句未启动。我应该将所选项目的类型转换为字符串吗?

///adapter for the payment frequency
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this,
R.array.paymentFrequency, android.R.layout.simple_spinner_item); //payment frequency is the name of the string in string.xml
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
frequency.setAdapter(adapter2);

frequency.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
paymentFrequency = String.valueOf(frequency.getSelectedItem());
Toast.makeText(Main2Activity.this, "choosen year:" + paymentFrequency, Toast.LENGTH_SHORT).show();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(Main2Activity.this, "nothing selected",Toast.LENGTH_SHORT).show();
}
});


public void calculate(View v){

if (paymentFrequency == "by-weekly"){
//the number of payment will be multiply by 24
n = Integer.parseInt(amortizasionPeriod)*24;
Toast.makeText(Main2Activity.this, "Number of payment:" + n, Toast.LENGTH_SHORT).show();
}
else if ( paymentFrequency == "monthly"){
//the number of payment will be multiply by 12
n = Integer.parseInt(amortizasionPeriod)*12;
Toast.makeText(Main2Activity.this, "Number of payment:" + n, Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(Main2Activity.this, "Error", Toast.LENGTH_SHORT).show();
}
}

最佳答案

在 Java 中查看两个字符串是否相等时,您需要使用 Java 中的 String.equals(String) 方法(请参阅: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#equals(java.lang.Object) )。因此,您应该拥有以下代码,而不是您拥有的代码:

if (paymentFrequency.equals("by-weekly")) {
//the number of payment will be multiply by 24
n = Integer.parseInt(amortizasionPeriod) * 24;
Toast.makeText(Main2Activity.this, "Number of payment:" + n, Toast.LENGTH_SHORT).show();
} else if (paymentFrequency.equals("monthly")) {
//the number of payment will be multiply by 12
n = Integer.parseInt(amortizasionPeriod) * 12;
Toast.makeText(Main2Activity.this, "Number of payment:" + n, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(Main2Activity.this, "Error", Toast.LENGTH_SHORT).show();
}

关于java - android studio 中旋转器的确切数据类型是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59638712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com