Android Spinner错误:无法应用AdapterView中的getOnItemSelectedListener

时间:2015-10-22 03:45:37

标签: java android android-spinner android-adapter android-adapterview

我使用一个简单的微调器来显示整数数组中的买入值。 Gradle Build中的错误说:method getOnItemSelectedListener in class AdapterView<T> cannot be applied to given types。没有必需的参数,错误的原因如下:actual and formal arguments lists differ in length where T is a type-variable: T extends Adapter declared in class AdapterView。我不确定如何解决(this)上显示的错误:

spinner.getOnItemSelectedListener(this);

我认为这是正确的做法,因为我告诉微调器,this活动负责监听微调器上的事件。

这是我的代码:

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

1 个答案:

答案 0 :(得分:1)

为什么你想要没有变量? 我想在这种情况下你应该使用spinner.setOnItemSelectedListener(this);而不是