Android - Spinner : how may I distinguish user'

2019-02-24 06:15发布

问题:

I'm in a trouble managing a spinner, so may I ask for your help ?

I have a spinner with its adapter. I initialize the spinner with a list of values when starting my activity. Then I force the selected value to be the one used in the object that I manage.

Once the screen is initialized : When the user selects a value in the spinner, according to the selected value, I may continue (or not) to another activity for let the user choose a complementary and necessary value. If the user "cancels" this second activity, I want to rollback the spinner to its previous selected value, and cancel some actions made in the meantime. If the user goes to the end of the second activity, everything is fine and I want juste to refresh the spinner display with the datas selected in the second activity (I overload the getView method in the adapter to do this).

Overall, I can easily do all of this, however, when I force the selected value in the spinner at the begining of my activity, or whene returning back from the second activity by "Cancel", the change value event is catched and the second activity is triggered (the user did not click anything at all).

How would you allow the second activity to be lauched only if the change of the selected value in the spinner is due to a manual action from the user, and prevent that same second activity to be launched when the value of spinner is changed "in the code "?

I tried many solutions, as setting a boolean into the adapter that tells if the next event will be raised because of an "in the code" action. Or also putting a boolean in the adapter that tells if the adapter has initialised itself, and I force that boolean to true on the forst change catched event. But nothing that really works fine.

Thank you for your help.

Oliver

回答1:

I've always solved that issue with boolean flags, it isnt pretty at all, but it works if you think it through.

The idea is more or less, create a global usable boolean and init with false, in the onSelectedItemListener() use that boolean to choose wether or not to trigger the action, the important thing to remember is to set it to true after the computer has selected it the first time automatically, and reset it to false in the onResume() method.

This isnt perfect but it should work.

Edit:

bool spinnerUsable1;
bool spinnerUsable2;
int positionSpinner;

public void onCreate(Bundle savedInstanceState){
   spinnerUsable1 = false;
   spinnerUsable2 = true;
   if(savedInstanceState != null){
      positionSpinner = savedInstanceState.getInt("posSpinner");
      if(positionSpinner != 0) spinnerUsable2 = false;
   }

   //Declare your spinner, set the on item selected lister
   spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                boolean spinnerUsable = (spinnerUsable1 && spinnerUsable2);
                if (!spinnerUsable1) {
                    spinnerUsable1 = true;
                } else if (!spinnerUsable2) {
                    spinnerUsable2 = true;
                }
                if (spinnerUsable) {
                    //Action;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // Nothing
            }
        });
}

Something like this should work.