Two spinners that cannot have the same selected value

Asked

Viewed 30 times

0

Hello, I have a code that has two spinners and that if we select an option in one spinner in the other that option ceases to exist , what I did was the following : Whenever you click on the spinner it clears and adds all and then removes what is selected in the other so that you cannot select the same in both! But when I select something in one, it enters an infinite loop, as if I were always performing 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) {
                    }

                });

            }

First Disciplines and Second Disciplines are the content of Spinners Second if Spinner First and Spinner Second respectively.

Thank you very much and a good evening or good afternoon for Brazilians!

1 answer

1


I think your algorithm should be: if I select something in the first Spinner, it changes the content of the second; and vice versa. The way you did, every time you make a choice on the first one, its content will be changed which amounts 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 don’t have android, so I didn’t test.

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

Scheme:

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;
            }
        }
    }
});

Try-Finally is to ensure, even in the case of an Exception, that selecionando be reset. Synchronization should not be a problem as these methods are always called from the same thread.

Obs:

this scheme is very unstable It might work better if you leave all options in the spinner and, if the user chooses the same option, simply disable the button that accepts the choice.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.