Reputation: 2252
I have this error in setOnItemSelectedListener
:
The method setOnItemSelectedListener(AdapterView.OnItemSelectedListener) in the type AdapterView is not applicable for the arguments (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..
Upvotes: 6
Views: 21488
Reputation: 15845
setOnItemSelectedListener
Must be implemented inside your FragmentMain
not inside your Activity
if you want to use the listener on the spinner inside the Fragment
inside your 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) {
}
});
Upvotes: 19
Reputation: 11323
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.
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:
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
}
});
Upvotes: 0