I have an error: setOnItemClickListener cannot be used with a spinner, what is wrong?

前端 未结 6 1543
感动是毒
感动是毒 2020-12-13 16:47

Kindly This is my code below, and I am pasting the error messages underneath: I am trying to use setOnItemClickListener on the spinner, is it permissible?

相关标签:
6条回答
  • 2020-12-13 17:11

    As @Alex very well said, you have to use

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    
            }
    
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
    
            }
        });
    

    Or if you are using ButterKnife:

    @OnItemSelected(R.id.spinner)
    public void onSpinnerItemSelected(int index){
        // ...
    }
    
    0 讨论(0)
  • 2020-12-13 17:12

    See the first line of your logcat:

    java.lang.RuntimeException: setOnItemClickListener cannot be used with a spinner.
    

    setOnItemClickListener cannot be used with a Spinner. Use setOnItemSelectedListener instead.

    0 讨论(0)
  • 2020-12-13 17:12

    Kotlin:

    spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
        override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
            print("onItemSelected position = $position id = $id")
        }
    
        override fun onNothingSelected(parent: AdapterView<*>) {
    
        }
    }
    

    I wrote it because inline creating object from the interface is different.

    0 讨论(0)
  • 2020-12-13 17:22

    Solved by

     spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int  
     i, long l) {
    
            }
    
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
    
            }
        });
    
    0 讨论(0)
  • 2020-12-13 17:28

    Change this

     convertto.setOnItemClickListener(new OnItemClickListener()
       {
    
        @Override
        public void onItemClick(AdapterView<?> arg, View v1, int index1, long arg4)
        {
             if ((quantity[index1]=="Meter") && (s1.equalsIgnoreCase("Meter")))
            {
    
            Toast.makeText(v1.getContext(), " to meter",Toast.LENGTH_SHORT).show();
            var2=var1;
            lresult.setText("" + var2);
            }
    
        }
    
       });
    

    to onItemSelectedListener,

     convertto.setOnItemSelectedListener(new OnItemSelectedListener() {
    
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
             if ((quantity[index1]=="Meter") && (s1.equalsIgnoreCase("Meter")))
            {
    
            Toast.makeText(v1.getContext(), " to meter",Toast.LENGTH_SHORT).show();
            var2=var1;
            lresult.setText("" + var2);
            }
    
    
        }
    
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
    
        }
    });
    

    It looks like you cannot use itemClickListener in your Spinner,

    Here is the error line which explains it,

    07-04 09:48:39.912: E/AndroidRuntime(694): Caused by: java.lang.RuntimeException: setOnItemClickListener cannot be used with a spinner.
    
    0 讨论(0)
  • 2020-12-13 17:28

    Simply change your implementation form setOnItemClickListener to setOnItemSelectedListener. Your code should work fine afterwards without error.

    Java:

    convertto.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    
            }
    
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
    
            }
        });
    

    Kotlin:

    convertto.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
    
        override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
    
        }
    
        override fun onNothingSelected(parent: AdapterView<*>) {
    
      }
    }
    
    0 讨论(0)
提交回复
热议问题