Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if the Spinner is click open on Android

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?

like image 615
user959974 Avatar asked Feb 11 '16 02:02

user959974


People also ask

How can I tell if spinner is clicked or not in android?

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.

How do you close a 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() .

What is the difference between spinner and ListView in android?

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.


1 Answers

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.

like image 106
Rakesh Avatar answered Sep 20 '22 06:09

Rakesh