Two spinners that can not have the same value selected

0

Hello, I have a code that has two spinners and if we select an option in a spinner in the other this option ceases to exist, what I did was: Whenever you click on the spinner it does clear and adds all and then removes what is selected in the other so that it can not be selected in both! But when I select something in one, it goes into an infinite loop, as if it were always doing the OnItemSelected method does anyone know why? Here is the code:

   disciplinasFirst.add("Biologia e Geologia");
                disciplinasFirst.add("Física e Química A");
                disciplinasFirst.add("Geometria Descritiva A");
                disciplinasFirst.add("Química");



                final ArrayAdapter<String> adapterDitsciplinasFirst = new ArrayAdapter<String>(ChoseDisciplinas2.this, R.layout.spinner_item_beggin, disciplinasFirst);

                adapterDitsciplinasFirst.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                SpinnerFirst.setAdapter(
                        new NothingSelectedSpinnerAdapter(
                                adapterDitsciplinasFirst,
                                R.layout.contact_spinner_row_nothing_selected,
                                // R.layout.contact_spinner_nothing_selected_dropdown, // Optional
                                this));


                disciplinasSecond.add("Biologia e Geologia");
                disciplinasSecond.add("Física e Química A");
                disciplinasSecond.add("Geometria Descritiva A");
                disciplinasSecond.add("Química");



                    final ArrayAdapter<String> adapterDisciplinasSecond = new ArrayAdapter<String>(ChoseDisciplinas2.this, R.layout.spinner_item_beggin, disciplinasSecond);

                adapterDisciplinasSecond.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                SpinnerSecond.setAdapter(
                        new NothingSelectedSpinnerAdapter(
                                adapterDisciplinasSecond,
                                R.layout.contact_spinner_row_nothing_selected,
                                // R.layout.contact_spinner_nothing_selected_dropdown, // Optional
                                this));

                SpinnerSecond.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        if (SpinnerFirst.getSelectedItem() != null) {
                            disciplinasSecond.clear();
                            disciplinasSecond.add("Biologia e Geologia");
                            disciplinasSecond.add("Física e Química A");
                            disciplinasSecond.add("Geometria Descritiva A");
                            disciplinasSecond.add("Química");
                            disciplinasSecond.remove(SpinnerFirst.getSelectedItem().toString());
                            final ArrayAdapter<String> adapterDisciplinasSecond = new ArrayAdapter<String>(ChoseDisciplinas2.this, R.layout.spinner_item_beggin, disciplinasSecond);
                            SpinnerSecond.setAdapter(adapterDisciplinasSecond);
                            Log.d(Tag,"fui selecionado2");
                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {
                    }

                });
                SpinnerFirst.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        if(SpinnerSecond.getSelectedItem()!=null){
                            disciplinasFirst.clear();
                            disciplinasFirst.add("Biologia e Geologia");
                            disciplinasFirst.add("Física e Química A");
                            disciplinasFirst.add("Geometria Descritiva A");
                            disciplinasFirst.add("Química");
                            disciplinasFirst.remove(SpinnerSecond.getSelectedItem().toString());
                            final ArrayAdapter<String> adapterDitsciplinasFirst = new ArrayAdapter<String>(ChoseDisciplinas2.this, R.layout.spinner_item_beggin, disciplinasFirst);
                            SpinnerFirst.setAdapter(adapterDitsciplinasFirst);

                        }
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {
                    }

                });

            }

Disciplines First and Disciplines Second are the content of the Spinners in this case Spinner First and Spinner Second respectively.

Thank you and a good evening or good afternoon to the Brazilians!

    
asked by anonymous 26.07.2017 / 23:45

1 answer

1

I think your algorithm should be: if I select something in the first Spinner, it changes the contents of the second; and vice versa. The way you did it, whenever you make a choice in the first one, the content of it will be changed which is equivalent to making a new selection by activating onItemSelected recursively.

But this algorithm can also cause a similar problem: when you select a value, onItemSelected will change the other spinner, whose onItemSelected will change the first, which in turn will again change the second, ... I do not have android, so I have not tested it.

Simple solution: use boolean as 'save' to prevent onItemSelected from making any changes if you already have another one running.

Layout:

private boolean selecionando = false;

SpinnerFirst.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {    
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        if (!selecionando) {
            selecionando = true;
            try {
                if (SpinnerFirst.getSelectedItem() != null) {
                    disciplinasSecond.clear();
                    disciplinasSecond.add("Biologia e Geologia");
                    ...
                    disciplinasSecond.remove(SpinnerFirst.getSelectedItem().toString());
                    ...
                }
            } finally {
                selecionando = false;
            }
        }
    }
});

SpinnerSecond.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        if (!selecionando) {
            selecionando = true;
            try {
                if (SpinnerSecond.getSelectedItem() != null) {
                    disciplinasFirst.clear();
                    disciplinasFirst.add("Biologia e Geologia");
                    ...
                    disciplinasFirst.remove(SpinnerSecond.getSelectedItem().toString());
                    ...
                }
            } finally {
                selecionando = false;
            }
        }
    }
});

The try-finally is to ensure, even in the case of an Exception, that selecionando is reset. Synchronization should not be a problem since these methods are always called from the same Thread.

Note:

This scheme is very unstable and complicated, maybe it works better if you leave all the options in the spinner and, if the user chooses the same option, simply disable the button that accepts the choice. >     

27.07.2017 / 00:07