Tin Tran
Tin Tran

Reputation: 6202

how to disable setOnItemSelectedListener(listener) Toast message

I have a listener to show a Toast message for when user clicks on a spinner and selects an item.

But when I set the value of the spinner programatically I want to disable the Toast message. How do i do it.

I have tried setOnItemSelectedListener(null) before programmatically setting the value of spinner then setOnItemSelectedListener(listener) after that but the Toast message still shows no matter what i do.

Thanks in advance.

This is all done inside onCreate btw. I just want to disable the Toast messaging when the onCreate sets default values for the spinner.

here's my listener

private OnItemSelectedListener listener = new OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view,
            int position, long id) {
        String item = (String) parent.getItemAtPosition(position);
        toast = Toast.makeText(getBaseContext(),
                Weather.TEMP_DESCRIPTION.get(item), Toast.LENGTH_SHORT);
        if (mTemp.getOnItemSelectedListener() != null) {
            toast.show();
        }

    };

    public void onNothingSelected(AdapterView<?> parent) {

    }
};

and here's what i have in onCreate()

Bundle extras = getIntent().getExtras();
        if (extras != null) {

            mTemp.setOnItemSelectedListener(null);

            String weather = extras.getString("weather");
            String tempStr = extras.getString("temp_str");
            if (weather.equals(Weather.UNAVAILABLE)) {
                mWeather.setSelection(adapter.getPosition(Weather.ANY_WEATHER));
                if (toast != null) {
                    toast.cancel();
                    toast.getView().setVisibility(View.INVISIBLE);
                    toast = null;
                }
            } else {
                mWeather.setSelection(adapter.getPosition(weather));
                mTemp.setSelection(tempAdapter.getPosition(tempStr));
                if (toast != null) {
                    toast.cancel();
                    toast.getView().setVisibility(View.INVISIBLE);
                    toast = null;
                }
            }

        }
mTemp.setOnItemSelectedListener(listener);

I tried many different things that's why you see all those weird if checks, i tried setting a boolean variable in the class too and it didn't work either.

Upvotes: 0

Views: 1201

Answers (4)

Phillip Dunley
Phillip Dunley

Reputation: 103

Spinners are tricky..Even when you programmatically set the value of a spinner, the onItemSelected will get called no matter what. So in onCreate, just set the value as you want and then call SetOnItemSelected.

Inside onItemSelected, you have to handle this case separately. You can use a flag, if flag is set, do nothing, else display toast. A counter can be used as well. Initiate the counter = 0; then use the following piece of code

  {
  if (spinner_count == 0)
  {
     spinner_count++;
  }
  else
  {
     //Display Toast message or whatever you want to do. 
  }

But this approach has a challenge. If the user selects what you have set programmatically, onItemSelected will not get called since android will not identify it as a change. In that case, you can decide to have a Submit button to supplement this. Or you can add another fake entry to your list of items in the spinner dropdown and set that as default option programmatically.

Upvotes: 0

hasan
hasan

Reputation: 24185

Global boolean:

public boolean userSet = true;

On Create:

Bundle extras = getIntent().getExtras();
    if (extras != null) {
        userSet = false;
        mTemp.setOnItemSelectedListener(null);

        String weather = extras.getString("weather");
        String tempStr = extras.getString("temp_str");
        if (weather.equals(Weather.UNAVAILABLE)) {
            mWeather.setSelection(adapter.getPosition(Weather.ANY_WEATHER));
            if (toast != null) {
                toast.cancel();
                toast.getView().setVisibility(View.INVISIBLE);
                toast = null;
            }
        } else {
            mWeather.setSelection(adapter.getPosition(weather));
            mTemp.setSelection(tempAdapter.getPosition(tempStr));
            if (toast != null) {
                toast.cancel();
                toast.getView().setVisibility(View.INVISIBLE);
                toast = null;
            }
        }

        mTemp.setOnItemSelectedListener(listener);
    }

Listener:

private OnItemSelectedListener listener = new OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view,
            int position, long id) {

       if (!userSet) {
           userSet = true;
           return;
       }
 }

Upvotes: 1

Kostas Drak
Kostas Drak

Reputation: 3260

You can do this

Toast toast = Toast.makeText(getApplicationContext(), "message", Toast.LENGTH_SHORT);

and when you need to show toast you use toast.show();

when you need to hide you use toast.cancel(); or dismiss or hide dont remember...

Upvotes: 0

You can call cancel() in the moment you set the value of the spinner programmatically to cancel the toast.

Upvotes: 0

Related Questions