【问题标题】:error help! syntax and OnItemSelectedListener错误帮助!语法和 OnItemSelectedListener
【发布时间】:2012-08-02 19:21:15
【问题描述】:

好的,所以我对此很陌生,但我明白了。我刚刚遇到了这个错误,我看到其他人也有同样的问题,但标准修复(干净)对我不起作用。我不知道如何解决这些错误!请帮忙!

第一个错误:

在我的 else if 语句中的 sp2.setOnItemSelectedListener(new OnItemSelectedListener() { 上,我不断收到此错误:

类型 new AdapterView.OnItemSelectedListener(){} 必须实现继承的抽象方法 AdapterView.OnItemSelectedListener.onNothingSelected(AdapterView)

我在那里有 onNothingSelected,它在我的 if 语句中有效,我的意思是我所做的只是复制、粘贴和编辑。

第二个错误:

}); 在我的 else if 语句末尾我收到错误:

语法错误,插入“;”完成声明

但一切都在那里。语句完成!

sp1.setOnItemSelectedListener(new OnItemSelectedListener() {

    public void onItemSelecteds(AdapterView<?> arg0, View arg1,
            int arg2, long arg3) {


             String reg_select = (sp1.getSelectedItem().toString());
                 if (reg_select.contentEquals("Southwest")){
                     sp2.setAdapter(sw_cit_adp); 
                     sp2.setOnItemSelectedListener(new OnItemSelectedListener() {

                            public void onItemSelected(AdapterView<?> arg0, View arg1,
                                    int arg2, long arg3) {
                                // TODO Auto-generated method stub

                             String sw_cit_select = (sp2.getSelectedItem().toString());
                             if (sw_cit_select.contentEquals("Lake Charles")){
                                 sp3.setAdapter(sw_lake_charles_adp); }

                             else if (sw_cit_select.contentEquals("Iowa")){
                                 sp3.setAdapter(sw_iowa_adp); }


                            ;}

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

                            };});}

                 else if (reg_select.contentEquals("South Central")){
                         sp2.setAdapter(sc_cit_adp); 
                         sp2.setOnItemSelectedListener(new OnItemSelectedListener() {

                                public void onItemSelected(AdapterView<?> arg0, View arg1,
                                        int arg2, long arg3) {
                                    // TODO Auto-generated method stub

                                 String sc_cit_select = (sp2.getSelectedItem().toString());
                                 if (sc_cit_select.contentEquals("Lafayette")){
                                     sp3.setAdapter(sc_lafayette_adp); }

                                 else if (sc_cit_select.contentEquals("Gueydan")){
                                     sp3.setAdapter(sc_gueydan_adp); }


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

                                }



                                ;
                               });
                         }

【问题讨论】:

    标签: android syntax android-spinner


    【解决方案1】:

    你在错误的地方使用了那个方法。你不能选择一个项目而不能选择任何东西,这是没有意义的。

    你需要把方法放在监听器下面,比如onItemSelected,但不要放在onItemSelected里面。

    这是它应该(基本上)看起来的样子:

        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
    
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int pos, long id) {
            // Your code to do something with the selected item
            }
        });
    

    哦,你需要使用确切的方法名称......它是onItemSelected 不是 onItemSelecteds

    【讨论】:

    • 查看我的编辑。您需要使用正确的方法名称并正确关闭调用(}); 而不仅仅是})。
    • 在 onItemSelecteds 修复中,我现在得到 Duplicate 方法 onItemSelected(AdapterView>, View, int, long) 类型为 new AdapterView.OnItemSelectedListener(){}
    • 是的,每个听众只能使用该方法一次
    • 我明白这一点,但我希望 sp2 依赖于 sp1 中的选择进行填充,而 sp3 依赖于 sp2 中的选择进行填充。它适用于 if 语句,它的 else if 语句给我带来了麻烦。这就是为什么我不明白。 if 语句是完美的,它的工作方式应该是这样的,所以我复制并粘贴了 if else 的代码,并在正确的位置对其进行了编辑以加载所需的信息,结果它就崩溃了。在我遇到所有这些错误之前,当我选择 Lafayette 时,我正在逼近,但我认为这是因为我没有定义动作
    • 对于 else if 语句,所以我去添加它并开始出错。
    猜你喜欢
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多