How to pass a selected spinner item between spinne

2019-08-12 14:37发布

问题:

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

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

        }
    });
}

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.

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

回答1:

I recommend you to use OnItemSelectedListener.

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

Of course you'll need to 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);
}

Then in your other Spinner use the FirstValue value.



回答2:

You should change itemSelectedInSpinnerLengthOutput as a global variable. After that, you can easy access to it in the onItemSelected method of the output spinner

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

            }
        });
    }

Hope this help