语法和OnItemSelectedListener args

时间:2013-05-16 09:21:27

标签: android

我使用Eclipse ADT build:v21.1.0-569685,Case One是来自网站的示例代码,但它会导致错误 “新的AdapterView.OnItemSelectedListener(){}类型必须实现继承的抽象方法AdapterView.OnItemSelectedListener.onNothingSelected(AdapterView)”

所以我必须使用案例二,但有时案例一是好的,发生了什么?谢谢!

案例一

private void InitSpinner(){
    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item,
            nameList.toArray(new String[nameList.size()]));
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerFolder.setAdapter(adapter1); 
    spinnerFolder.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
            Toast.makeText(MainActivity.this,
                    "cw", 2000).show();

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

        }
    }); 
}

案例二

private void InitSpinner(){
    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item,
            nameList.toArray(new String[nameList.size()]));
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerFolder.setAdapter(adapter1); 
    spinnerFolder.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            Toast.makeText(MainActivity.this,
                    "cw", 2000).show();

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    }); 
}

1 个答案:

答案 0 :(得分:1)

@保罗

参数名称必须相同吗?

不,参数名称不必相同。因此,这两个代码完全相同,并且将以相同的方式工作。

为什么我

 The type new AdapterView.OnItemSelectedListener(){} must implement the inherited abstract method AdapterView.OnItemSelectedListener.onNothingSelected(AdapterView)

错误吗

我猜您必须从其他项目中复制该部分代码,或者必须通过某个站点直接从Internet复制。在这里,您已经复制了正确的语法,但没有为同一个进行正确的导入....

尝试..

Ctrl + Shift + O 以整理所有必需的导入...

希望它有所帮助.....