简体   繁体   English

如何从Spinner侦听器调用方法-Android

[英]How to call a method from a Spinner listener - Android

I would like to auto run a conversion method when a new unit is selected from the dropdown Spinner menu. 从下拉菜单微调器中选择一个新单位时,我想自动运行一种转换方法。 The code works ok to update a TextView object with the newly selected option so the listener is working ok. 该代码可以正常工作,以使用新选择的选项更新TextView对象,因此侦听器可以正常工作。 When I add a method call it crashes and the app won't even start, any ideas why this might be? 当我添加一个方法调用时,它崩溃并且该应用甚至无法启动,为什么会有这种想法?

spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String str = ((TextView) view).getText().toString();
            unitsInput.setText(str); 

            convert();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        } 

    });

I think I have found the problem my initial value in the input space was a blank string "" and it was not happy about it on start up. 我想我已经发现问题,我在输入空间中的初始值是一个空字符串“”,启动时对此并不满意。

Log: 日志:

 06-25 10:36:47.340: E/AndroidRuntime(1312): java.lang.NumberFormatException: Invalid double: ""
06-25 10:36:47.340: E/AndroidRuntime(1312):     at java.lang.StringToReal.invalidReal(StringToReal.java:63)
06-25 10:36:47.340: E/AndroidRuntime(1312):     at java.lang.StringToReal.parseDouble(StringToReal.java:248)
06-25 10:36:47.340: E/AndroidRuntime(1312):     at java.lang.Double.parseDouble(Double.java:295)

Setting the initial value to 1 in the input EditText space has solved the problem and allowed the app to run again and run the convert() method upon selecting something from the drop down spinner. 在输入EditText空间中将初始值设置为1已解决了该问题,并允许该应用再次运行,并在从下拉微调器中选择某些内容后运行convert()方法。

However, if was to leave the initial input blank or empty "" then it still does not work. 但是,如果将初始输入留为空白或为空“”,则它仍然不起作用。 If i include an if statement clause like 如果我包括if语句子句

 if(!"".equals(str)){
                convert();
            }

or 要么

 if(!str.equals("")){
                convert();
            }

It crashes on launch. 它在启动时崩溃。

Does anyone know how to implement the method call using a Spinner Listener and a blank String? 有谁知道如何使用Spinner Listener和空白String来实现方法调用?

Try this.. 尝试这个..

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String str = ((TextView) view).getText().toString();
        unitsInput.setText(str); 

        if(!str.equals(""))
             convert();
    }

Just as an update I got this working as I wanted using the following code for anyone with a similiar problem 作为更新,我可以按需要为有类似问题的任何人使用以下代码来完成此工作

 spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String str = ((TextView) view).getText().toString();
            unitsInput.setText(str); 

            if(!"".equals(valueInput.getText().toString())){
                convert();
            }
        }

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

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