Android 片段中微调器的 setOnItemSelectedListener
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23449270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
setOnItemSelectedListener for spinner in Fragment
提问by Mounir Elfassi
I have this error in setOnItemSelectedListener
:
我有这个错误setOnItemSelectedListener
:
The method setOnItemSelectedListener(AdapterView.OnItemSelectedListener) in the type AdapterView is not applicable for the arguments (FragmentMain)"
AdapterView 类型中的 setOnItemSelectedListener(AdapterView.OnItemSelectedListener) 方法不适用于参数 (FragmentMain)"
Fragment Class :
片段类:
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
}
}
This is my 1st post here, so excuse/correct me PLEASE. Sorry for my English..
这是我在这里的第一篇文章,所以请原谅/纠正我。对不起我的英语不好..
回答by Silverstorm
setOnItemSelectedListener
Must be implemented inside your FragmentMain
not inside your Activity
if you want to use the listener on the spinner inside the Fragment
如果您想在 Fragment 内的微调器上使用侦听器,则必须在您的FragmentMain
内部实现而不是在您Activity
的内部实现
inside your FragmentMain onCreateView
在您的 FragmentMain onCreateView 中
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
}
put this code
把这个代码
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) {
}
});
回答by donfuxx
The problem is this line here:
问题是这里的这一行:
countriesSpinner.setOnItemSelectedListener(this);
because "this" refers to the Fragment in your case, but you should pass in an OnItemSelectedListener object.
因为“this”在您的情况下是指 Fragment,但您应该传入 OnItemSelectedListener 对象。
Also there seems to be quite a confusion about how Listeners work. You probably wanted to implement OnItemSelectedListener in your Fragment instead of creating that CustomOnItemSelectedListener
Activity. Or alternatively create an anonymous OnItemSelectedListener:
此外,关于 Listeners 的工作方式似乎也很混乱。您可能希望在 Fragment 中实现 OnItemSelectedListener 而不是创建该CustomOnItemSelectedListener
Activity。或者创建一个匿名 OnItemSelectedListener:
countriesSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});