Search code examples
javaandroidandroid-spinnerandroid-adapterandroid-adapterview

Android Spinner error: getOnItemSelectedListener in AdapterView cannot be applied


I am using a simple spinner to display buy-in values in an integer-array. The error in the Gradle Build says: method getOnItemSelectedListener in class AdapterView<T> cannot be applied to given types. There are no required arguments and the reason for the error is given as: actual and formal arguments lists differ in length where T is a type-variable: T extends Adapter declared in class AdapterView. I am not sure how to resolve the error which appears on (this):

spinner.getOnItemSelectedListener(this);

I would assume that this is correct practice as I am telling the spinner that this activity is responsible for listening to the events on the spinner.

Here is my code:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


public class HomeActivity extends Activity implements AdapterView.OnItemSelectedListener{

    Spinner spinner;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // Get the view from home_activity.xml
        setContentView(R.layout.home_activity);

        // initialize spinner
        spinner = (Spinner) findViewById(R.id.buyInSpinner);

        ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.buy_in, android.R.layout.simple_spinner_item);
        spinner.setAdapter(adapter);
        spinner.getOnItemSelectedListener(this);

    } // end onCreate method

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View v, int i, long l)
    {
        TextView myText = (TextView) v;
        Toast.makeText(this, "You Selected "+myText.getText(), Toast.LENGTH_SHORT).show();
    } // end onItemSelected method

    @Override
    public void onNothingSelected(AdapterView<?> adapterView)
    {
        Toast.makeText(this, "You Must Buy-In To Play", Toast.LENGTH_SHORT).show();
    } // end onNothingSelected method

} // end HomeActivity class

Solution

  • Why do you want to get without a variable? I think in this case you should use spinner.setOnItemSelectedListener(this); instead