Android:从onItemSelected()方法

时间:2017-07-29 20:47:50

标签: android spinner

我遇到了以下问题:

请参阅我的代码

public void onItemSelected(AdapterView parent,View view,int position,long id){

    Spinner spinner = (Spinner) findViewById(R.id.spinnerMonth);
    Spinner spinner2 = (Spinner) findViewById(R.id.spinnerDay);

    switch (parent.getId())
    {
        case R.id.spinnerMonth:
            String monthstring = spinner.getItemAtPosition(position).toString();
            switch (monthstring)
            {
                case "Januar": month = 1; break;
                case "Februar": month = 2; break;
                case "März": month = 3; break;
                case "April": month = 4; break;
                case "Mai": month = 5; break;
                case "Juni": month = 6; break;
                case "Juli": month = 7; break;
                case "August": month = 8; break;
                case "September": month = 9; break;
                case "Oktober": month = 10; break;
                case "November": month = 11; break;
                case "Dezember": month = 12; break;
            }
            break;
        case R.id.spinnerDay:
            day = Integer.parseInt(spinner2.getItemAtPosition(position).toString());
            break;
    }

    TextView textDate = (TextView) findViewById(R.id.textDate);
    textDate.setText(""+month+"---"+day);
}

private void setEvents(){

    String date = ""+day+"."+month+".";
    //String date = ""+this.getDate();
    textLine1.setText(date);

在onItemSelected方法中,我得到了月和日的正确值 - 但是在setEvents方法中,我获得了0和0的月和日值。这两个变量都被声明为全局变量。

我在哪里错过了什么?

感谢您帮助我 迈克尔

1 个答案:

答案 0 :(得分:0)

从微调器中选择月份和日期时,尝试调用onEtemSelected上的setEvents()。