如何获得在Android的微调选定的项目如何获得、项目、Android

2023-09-07 23:44:47 作者:∞骨子里傲

我想提出一个货币转换器​​,具有两个微调。我想要一个如果使用类似下面的微调器的选择项的值功能,使。

I am making a currency converter with two spinners. I want to make an "if" function using the values of the spinner's selected item like below.

    @Override
    public void onClick(View v) {
        if (spinner1.getSelectedItem()=="Dollars" && spinner2.getSelectedItem()=="Euros") {
            convertDollarstoEuros();
        }
        if (spinner1.getSelectedItem()=="Euros" && spinner2.getSelectedItem()=="Euros") {
            convertEurostoEuros();
        }
    Toast.makeText(MainActivity.this,
            "OnClickListener : " + 
                    "\nSpinner 1 : "+ String.valueOf(spinner1.getSelectedItem()) + 
                    "\nSpinner 2 : "+ String.valueOf(spinner2.getSelectedItem()),
                Toast.LENGTH_SHORT).show();
        }

问题是,敬酒是显示,但货币不会转化。敬酒部分工作,但微调的部分是没有的。任何帮助将大大AP preciated。这是我的LogCat中:

The problem is that the toast is showing, but the currencies aren't converting. The toast part is working, but the spinner part isn't. Any help would be greatly appreciated. Here is my LogCat:

推荐答案

试试这个:

if (spinner1.getSelectedItem().toString().equals("Dollars") && spinner2.getSelectedItem().toString().equals("Euros")
...

getSelectedItem()返回一个对象。 info 。所以,你必须首先得到相应的字符串。 那么Java使用等于比较字符串()

getSelectedItem() returns an Object . info . So you have to get the corresponding string first. Then java compares strings using equals().