Android Studio Spinner代码

时间:2018-03-21 19:20:57

标签: java android android-spinner

请有人在附加的微调器代码中告诉我我的错误。它崩溃了应用程序

 String arr[] = {"Maths", "Biology"};
 ArrayAdapter a = new ArrayAdapter(this,android.R.layout.simple_spinner_item, arr);
 Spinner spin = (Spinner) findViewById(R.id.spin1);
 spin.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) this);
 spin.setAdapter(a);

1 个答案:

答案 0 :(得分:1)

您无法为AdapterView.OnItemSelectedListener

转换活动
spin.setAdapter(a);
spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
             //Get position and the related item from here
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                //Another interface callback
            }
        });