seyid yagmur seyid yagmur - 5 months ago 12
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();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.present);

FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.anim.fade_in,android.R.anim.fade_out);

ft.replace(R.id.flPersonalization, question1Fragment);

ft.commit();
}


and This is my Questions class.it 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;

@Override
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(R.id.listLayout);
spinner=(SearchableSpinner)view.findViewById(R.id.spinner1);
}


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

Answer

You could do something like this:

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

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
});
Comments