0

I have conditional spinners that I'm using along with a switch case. i.e. Spinner1-choice1 will output Spinner2-a/b/c as options, and Spinner1-choice2 will output Spinner2-x/y/z. I'm trying to assign unique int identifiers to the parent spinner's choice, and have a switch case to display the child spinner based on the output of the parent spinner. Currently the parent spinner works and displays the value of selectedCounty in a textview, but I cannot get the switch case to read the value.

Here's a snippet of my code:

countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view,
                               int position, long id) {
        int innerCounty = countySpinner.getSelectedItemPosition();
        switch (innerCounty) {
            case 0: {
                selectedCounty = 20;

                break;
            }
            case 1: {
                selectedCounty = 25;

                break;
            }
            case 2: {
                selectedCounty = 27;
                break;
            }
            case 3: {
                selectedCounty = 43;
                break;
            }
            case 4: {
                selectedCounty = 60;
                break;
            }
            case 5: {
                selectedCounty = 61;
                break;
            }
            default: {
               // selectedCounty = 0;
            }
        }
        countyID.setText(String.valueOf(selectedCounty));  
    }

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

As you can see, selectedCounty is the variable I am trying to output. I then have a separate switch case that switches selectedCounty, here's a snippet for example:

switch (selectedCounty){
    ....
    case 20: {
        Toast.makeText(getActivity(),"Crawford",Toast.LENGTH_SHORT).show();
        ArrayAdapter<String> Madapter = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.crawford));
        Madapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        municipalitySpinner.setAdapter(Madapter);
        break;
}   

My question is, how can I return the value of selectedCounty in order to be used by the switch case?

I feel like something like this is what I want, but when I change void to int in the statement for onItemSelected, I get "attempting to use incompatible return type"

countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public **int** onItemSelected(AdapterView<?> parent, View view,
                               int position, long id) {
        int innerCounty = countySpinner.getSelectedItemPosition();
        switch (innerCounty) {
            case 0: {
                selectedCounty = 20;
                break;
            }
            case 1: {
                selectedCounty = 25;
                break;
            }
            case 2: {
                selectedCounty = 27;
                break;
            }
            case 3: {
                selectedCounty = 43;
                break;
            }
            case 4: {
                selectedCounty = 60;
                break;
            }
            case 5: {
                selectedCounty = 61;
                break;
            }
            default: {
               // selectedCounty = 0;
            }
        }
        countyID.setText(String.valueOf(selectedCounty));  

        **return selectedCounty;**
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
    }
});
Saurabh
  • 434
  • 1
  • 4
  • 12
JHinne
  • 9
  • 6

0 Answers0