我在setOnItemSelectedListener
:
AdapterView类型中的方法setOnItemSelectedListener(AdapterView.OnItemSelectedListener)不适用于参数(FragmentMain)"
片段类:
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
}
}
这是我在这里的第一篇文章,请原谅/纠正我请。 对不起我的英文..
答案 0 :(得分:16)
setOnItemSelectedListener
如果您想在片段内的微调器上使用侦听器
,则必须在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) {
}
});
答案 1 :(得分:0)
问题是这一行:
countriesSpinner.setOnItemSelectedListener(this);
因为&#34;这个&#34;在你的情况下引用Fragment,但是你应该传入一个OnItemSelectedListener对象。
对于听众的工作方式似乎也存在很多困惑。您可能希望在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
}
});