如何在微调器之间传递选定的微调器项目?

时间:2015-09-27 09:17:25

标签: java android methods spinner android-spinner

经过彻底的搜索和大量的思考后,我无法在AndroidStudio中找到解决以下问题的方法:

我有2个微调器(输入和输出)。我想将输入微调器的值传递给在选择输出微调器(onItemSelected)的值时调用的方法。相关代码段落如下:

private void setupSpinnerListeners() {

    spinnerLengthInput.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String itemSelectedInSpinnerLengthInput = parent.getItemAtPosition(position).toString();
            checkIfConvertingFromMeter(itemSelectedInSpinnerLengthInput);
        }

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

        }
    });

    spinnerLengthOutput.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String itemSelectedInSpinnerLengthOutput = parent.getItemAtPosition(position).toString();
            updateOutputTextfield(itemSelectedInSpinnerLengthInput, itemSelectedInSpinnerLengthOutput);
        }

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

        }
    });
}

我希望String itemSelectedInSpinnerLengthInput(从输入微调器获取其值)在输出微调器的onItemSelected方法中可用。怎么做到这一点? 非常感谢任何帮助。

编辑:在setupSpinnerListeners方法中创建一个全局变量,这是一个长度为1的数组。它将按照我的预期工作。

2 个答案:

答案 0 :(得分:0)

我建议您使用OnItemSelectedListener

然后创建一个globalVariable,将console.log送到您的第一个String,如下所示:

Spinner

然后你需要打电话给:

String FirstValue = "";

当然,您需要spinnerLengthInput.setOnItemSelectedListener(this); spinnerLengthOutput.setOnItemSelectedListener(this);

然后在里面你可以做你正在做的事情。

implements OnItemSelectedListener

然后在您的其他 @Override public void onItemSelected(AdapterView<?> spinner, View view, int position,long id) { FirstValue = spinner.getItemAtPosition(position).toString(); checkIfConvertingFromMeter(itemSelectedInSpinnerLengthInput); } 中使用Spinner值。

答案 1 :(得分:0)

您应该将 itemSelectedInSpinnerLengthOutput 更改为全局变量。之后,您可以在输出微调器

的onItemSelected方法中轻松访问它
String itemSelectedInSpinnerLengthInput; // global variable
    private void setupSpinnerListeners() {

        spinnerLengthInput.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                itemSelectedInSpinnerLengthInput = parent.getItemAtPosition(position).toString();
                checkIfConvertingFromMeter(itemSelectedInSpinnerLengthInput);
            }

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

            }
        });

        spinnerLengthOutput.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String itemSelectedInSpinnerLengthOutput = parent.getItemAtPosition(position).toString();
                if(itemSelectedInSpinnerLengthInput != null){
                        updateOutputTextfield(itemSelectedInSpinnerLengthInput, itemSelectedInSpinnerLengthOutput);
                }else{
                     Toast.makeText(getApplicationContext(), "please select input", Toast.LENGTH_LONG).show();
                     ...
                }
            }

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

            }
        });
    }

希望这个帮助