android - Fragment 中微调器的 setOnItemSelectedListener

标签 android android-fragments android-spinner

我在 setOnItemSelectedListener 中有这个错误:

The method setOnItemSelectedListener(AdapterView.OnItemSelectedListener) in the type AdapterView is not applicable for the arguments (FragmentMain)"

fragment 类:

public class FragmentMain extends Fragment  {

private Spinner countriesSpinner;
private Activity rootView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
     View rootView =inflater.inflate(R.layout.activity_main, container, false);
     return rootView;
}
@Override
public void onStart() {
    super.onStart();

    addItemsOnSpinner();
}
public void addItemsOnSpinner() {

    countriesSpinner = (Spinner) rootView.findViewById(R.id.team_list_spinner);
    countriesSpinner.setOnItemSelectedListener(new  CustomOnItemSelectedListener ()) ;

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
            R.array.team_list, android.R.layout.simple_spinner_item);

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    countriesSpinner.setAdapter(adapter);

    countriesSpinner.setOnItemSelectedListener(this);

    }

public class CustomOnItemSelectedListener extends Activity implements
OnItemSelectedListener {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos,
            long id)  {
        // TODO Auto-generated method stub
        if (parent.getItemAtPosition(pos).toString()
                .equals("San Antonio Spurs")) {
            Intent i = new Intent(getApplicationContext(), Spurs_games.class);
            startActivity(i);
            finish();}
        if (parent.getItemAtPosition(pos).toString()
                .equals("Los Angeles Lakers")) {
            Intent i = new Intent(getApplicationContext(), Lakers_games.class);
            startActivity(i);
            finish();}
    }

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

    }

}

public void onItemSelected(AdapterView<?> parent, View view, int pos,
        long id) {
    if (parent.getItemAtPosition(pos).toString()
            .equals("San Antonio Spurs")) {
        Intent i = new Intent(getActivity(), Spurs_games.class);
        startActivity(i);
        finish();}
    if (parent.getItemAtPosition(pos).toString()
            .equals("Los Angeles Lakers")) {
        Intent i = new Intent(getActivity(), Lakers_games.class);
        startActivity(i);
        finish();}
}

private void finish() {
    // TODO Auto-generated method stub

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

}
}  

这是我在这里的第一篇文章,所以请原谅/纠正我。 对不起我的英语..

最佳答案

setOnItemSelectedListener

如果您想在 Fragment 内的微调器上使用监听器,则必须在您的 FragmentMain 中实现,而不是在您的 Activity 中实现

在你的 FragmentMain onCreateView 中

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
...
}

输入这段代码

countriesSpinner = (Spinner) rootView.findViewById(R.id.team_list_spinner);
countriesSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

关于android - Fragment 中微调器的 setOnItemSelectedListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23449270/

相关文章:

android - 如何更改操作栏 NAVIGATION_MODE_LIST 中的 'little triangle color'

android - 我没有使用 Retrofit Android 从 REST API 获得响应

java - Google 云端点(应用程序引擎)+ oauth2 与 android 集成

android - Livedata 观察者多次触发

java - 带有 TabLayout 和 ViewPager 的 fragment

java - 在 Android Studio 中添加 Spinner 时遇到问题

java - 面对 process.start(); 的问题在安卓 Lollipop 中

java - 如何使用 AlarmManager 将闹钟设置为每个月的第一个星期四

android - FragmentManager popBackStack 不起作用 - 错误?

android - 在微调器中,选择特定项目时背景不会改变