Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setOnItemSelectedListener for spinner in Fragment

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..

like image 866
Mounir Elfassi Avatar asked May 03 '14 20:05

Mounir Elfassi


1 Answers

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) {

            }
        });
like image 170
Silverstorm Avatar answered Nov 17 '22 16:11

Silverstorm