android - 使用微调器从onItemSelected返回int

时间:2014-09-17 19:23:19

标签: android android-fragments android-spinner android-switch

我有条件旋转器,我和切换盒一起使用。即Spinner1-choice1将输出Spinner2-a / b / c作为选项,Spinner1-choice2将输出Spinner2-x / y / z。我试图将唯一的int标识符分配给父微调器的选择,并且有一个switch case来根据父微调器的输出显示子微调器。目前,父级微调器工作并在textview中显示selectedCounty的值,但我无法通过switch case来读取值。

这是我的代码片段:

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

如您所见,selectedCounty是我想要输出的变量。然后我有一个单独的开关盒,可以切换selectedCounty,这里是一个片段,例如:

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

我的问题是,如何才能返回selectedCounty的值才能被切换案例使用?

我觉得这就是我想要的东西,但是当我在onItemSelected的陈述中将 void 更改为 int 时,我得到了#34 ;尝试使用不兼容的返回类型&#34;

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

0 个答案:

没有答案