seyid yagmur seyid yagmur - 2 years ago 67
Android Question

How can I use frament's spinner events in my main class?

the code below named Presenteter is my main class.and I am replacing the fragments acording to questions.

public class Presenteter extends AppCompatActivity {
private final Questions question1Fragment = new Questions();
protected void onCreate(Bundle savedInstanceState) {

FragmentTransaction ft=getSupportFragmentManager().beginTransaction();

ft.replace(, question1Fragment);


and This is my Questions is getting the questions from REST server.all questions listenin in one spinner.I didn't wrote them here, in order not to you confuse.

public class Questions extends Fragment implements AdapterView.OnItemSelectedListener {
SearchableSpinner spinner;

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.question, container, false);
LinearLayout ln=(LinearLayout)view.findViewById(;

finally I want to use spinner's onItemSelected event in my Presenter class, how can I do this.
thanks in advance.

Answer Source

You could do something like this:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        Presenteter p = (Presenteter) getActivity();
        p.yourMethod(); // call a method of your Presenteter class

    public void onNothingSelected(AdapterView<?> parent) {

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download