I have a spinner and I want to detect when the user clicked on it. When the user clicks it open, I want to call an endpoint and then update the spinner with new data. But I couldn't figure out how to do this and results in infinite loop:
I call the API in here:
public View getDropDownView(int position, View convertView, ViewGroup parent) {
// Call API here
// Do render with old data.
}
when the API successes, I call the to update:
@Override
public void onSuccess(final Response response) {
Helper.update(...);
}
public void update(...) {
...
adapter.setItems(newData);
adapter.notifyDataSetChanged();
}
and then this triggers getDropDownView()
again. I couldn't figure out what else is called after the user clicks open the spinner besides getDropDownView()
. How can I solve this problem?
Let the Spinner's first value be something like "-please select-". when the user clicks on the next button perform validation and check whether the value of the selectedItem in the spinner is "-please select-" and if yes, then display a toast and ask the the user to select something from the spinner.
onDetachedFromWindow(); which does the job of closing the prompt dialog for you. Now that being said, calling spin. onDetachedFromWindow(); from anywhere in your Activity should help you to close the spinner programatically. So if this is what you want, then remove the onpause() .
Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one. ListView is a view group that displays a list of scrollable items.
You can set Touch-listener on Spinner to detect click.
findViewById(R.id.spinner).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(MainActivity.this,"CallAPI",Toast.LENGTH_SHORT).show();
return false;
}
});
And return False from onTouch to behave normally.I hope It will work for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With