如何在微调器之间传递选定的微调器项目? [英] How to pass a selected spinner item between spinners?

查看:109
本文介绍了如何在微调器之间传递选定的微调器项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

After a thorough search and quite a lot of thinking, I couldn't find a solution to the following problem in AndroidStudio:

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

I have 2 spinners (input and output). I want to pass the value of the input spinner to a method that is called upon selection of a value of the output spinner (onItemSelected). The regarding code passage looks as follows:

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) {

        }
    });
}

我希望在输出微调器的onItemSelected方法中可以使用String itemSelectedInSpinnerLengthInput(从输入微调器获取其值).如何做到这一点? 任何帮助,我们将不胜感激.

I want the String itemSelectedInSpinnerLengthInput (that gets its value from the input spinner) to be available in the onItemSelected method of the output spinner. How to accomplish this? Any help is greatly appreciated.

在setupSpinnerListeners方法内部创建一个全局变量,它是一个长度为1的数组.它将按预期工作.

Create a global variable INSIDE the setupSpinnerListeners Method, that is an array with length 1. The it'll work as I had intended.

推荐答案

我建议您使用

I recommend you to use OnItemSelectedListener.

然后创建一个globalVariable,将String移至您的第一个Spinner,如下所示:

Then create a globalVariable to get the String to your first Spinner as follows :

String FirstValue = "";

然后您需要调用此名称:

Then you'll need to call this :

spinnerLengthInput.setOnItemSelectedListener(this);
spinnerLengthOutput.setOnItemSelectedListener(this);

当然,您需要implements OnItemSelectedListener

然后,您可以在内部执行与以前相同的操作.

Then inside you can do the same that you were doing.

 @Override
public void onItemSelected(AdapterView<?> spinner, View view, int position,long id)
{
     FirstValue = spinner.getItemAtPosition(position).toString();
        checkIfConvertingFromMeter(itemSelectedInSpinnerLengthInput);
}

然后在其他Spinner中使用FirstValue值.

这篇关于如何在微调器之间传递选定的微调器项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆