Java – how to use fragment’s Spinner event in the main class?
•
Android
The following code named presenter is my main course. I will replace fragment according to the problem
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();
}
This is my problem class. It is getting problems from the rest server. All the problems are listed in a spinner. I didn't write them here to avoid confusing you
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 the presenter class
How can I do this? Thank you in advance
resolvent:
You can do the following in a clip:
Presenteter p = (Presenteter) getActivity();
// ...
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
p.yourMethod(); // call a method of your Presenteter class
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码