I am writing an application in Android Studio. I want to implement two different students in the same class.
Here is a part of my code:

MyFragment extends Fragment implements AdapterView.OnItemSelectedListener, View.OnClickListener { //my fields private Spinner mySpinner; @Ovveride public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) { //мой код } @Ovveride public void onNothingSelected(AdapterView<?>p){} @Ovveride public void onClick(View v) { //мой код } @Ovveride public void onCreate(Bundle b) { super.onCreate(b); //мой код mySpinner = (Spinner) fragmentView.findViewById(R.id.my_spinner); mySpinner.setOnItemSelectedListener(this); 

On the last line, I received an error:

'setOnItemSelectedListener (android.widget.AdapterView.OnItemSelectedListener)' in 'android.widget.AdapterView' cannot be applied to '(anonymous android.view.View.OnClickListener)'

How to fix this error?
On the enSO write that you can do that.

  • The checkbox is not a class that works with the adapter (as opposed to spinner on your link) and you cannot use this listener with a simple View, which is what you said in the error. OnCheckedChangeListener listener is used for checkbox - pavlofff
  • @pavlofff, I apologize, rewrote the code incorrectly. OnItemSelectedListener I hung on Spinner. - Real KEK

2 answers 2

If your myCheckBox is a CheckBox then you can myCheckBox an action in this way

 myCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if ( isChecked ) { // perform logic } } }); 

The AdapterView.OnItemSelectedListener interface refers to a spinner . The onItemSelected method works only on clicking on an item in the spinner

  • This is clear to me. I am interested in, is it possible to use that way? - Real KEK
  • one
    MyFragment can implement as many listeners as you want - pavel163
  • c multiple listeners not compiled. Gives an error message. Maybe I did something wrong? - Real KEK
  • In fact, the onItemSelected method works by clicking on an item not only in a spinner , but in any AdapterView , for example, in a ListView . - Real KEK
  • I meant the example that was left in the link - pavel163

I got lost, I have a line mySpinner.setOnItemSelectedListener(this); was in a local class, which is what is written in the error, so I had to write MyFragment.this .