Does Spinner OnItemSelectedListener not work?

1

As such, it does run, but the if and else if statements do not run and I can not find a solution to debug

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

boolean IntersticialBoolean = true;
InterstitialAd AnuncioIntersticial;

/*----------------------------------------*/
int Banda1;
int Banda2;
int Banda3;

String Union = "";

int Multipler;
int Divider;
int T;

String Tolerance = "";
String PPM = "";
/*----------------------------------------*/
Spinner spinner1, spinner2, spinner3, spinnerALL, spinnerTolerance, spinnerPPM, SpinerOhm;
RadioButton RB1, RB2, RB3;
TextView TV1, TV2, TV3, TVALL, TVTolerance, TVPPM, TextoResultado, TextoResultadoKOhm;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    final AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

    FirebaseCrash.log("CRECrash");

    mAdView.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            mAdView.setVisibility(View.VISIBLE);
        }
    });

    PublicidadIntersticial();

    spinner1 = (Spinner) findViewById(R.id.Spinner1);
    spinner2 = (Spinner) findViewById(R.id.Spinner2);
    spinner3 = (Spinner) findViewById(R.id.Spinner3);
    spinnerALL = (Spinner) findViewById(R.id.SpinnerALL);
    spinnerTolerance = (Spinner) findViewById(R.id.SpinnerTolerance);
    spinnerPPM = (Spinner) findViewById(R.id.SpinnerPPM);
    SpinerOhm = (Spinner) findViewById(R.id.Ohm);

    RB1 = (RadioButton) findViewById(R.id.RB1);
    RB2 = (RadioButton) findViewById(R.id.RB2);
    RB3 = (RadioButton) findViewById(R.id.RB3);

    TV1 = (TextView) findViewById(R.id.TV1);
    TV2 = (TextView) findViewById(R.id.TV2);
    TV3 = (TextView) findViewById(R.id.TV3);
    TVALL = (TextView) findViewById(R.id.TVALL);
    TVTolerance = (TextView) findViewById(R.id.TVTolerance);
    TVPPM = (TextView) findViewById(R.id.TVPPM);
    TextoResultado = (TextView) findViewById(R.id.TextoResultado);
    TextoResultadoKOhm = (TextView) findViewById(R.id.TextoResultadoKOhm);

    ArrayAdapter<CharSequence> Colors = ArrayAdapter.createFromResource(this, R.array.Colors, android.R.layout.simple_spinner_item);
    ArrayAdapter<CharSequence> ColorsALL = ArrayAdapter.createFromResource(this, R.array.ColorsALL, android.R.layout.simple_spinner_item);
    ArrayAdapter<CharSequence> ColorsTolerance = ArrayAdapter.createFromResource(this, R.array.ColorsTolerance, android.R.layout.simple_spinner_item);
    ArrayAdapter<CharSequence> ColorsPPM = ArrayAdapter.createFromResource(this, R.array.ColorsPPM, android.R.layout.simple_spinner_item);
    ArrayAdapter<CharSequence> Omega = ArrayAdapter.createFromResource(this, R.array.Ohm, android.R.layout.simple_spinner_item);

    spinner1.setAdapter(Colors);
    spinner2.setAdapter(Colors);
    spinner3.setAdapter(Colors);
    spinnerALL.setAdapter(ColorsALL);
    spinnerTolerance.setAdapter(ColorsTolerance);
    spinnerPPM.setAdapter(ColorsPPM);
    SpinerOhm.setAdapter(Omega);

    spinner1.setOnItemSelectedListener(this);
    spinner2.setOnItemSelectedListener(this);
    spinner3.setOnItemSelectedListener(this);
    spinnerALL.setOnItemSelectedListener(this);
    spinnerTolerance.setOnItemSelectedListener(this);
    spinnerPPM.setOnItemSelectedListener(this);
    SpinerOhm.setOnItemSelectedListener(this);

    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.RG);
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            if (RB1.isChecked()) {
                spinner1.setVisibility(View.VISIBLE);
                spinner2.setVisibility(View.VISIBLE);
                spinner3.setVisibility(View.GONE);
                spinnerALL.setVisibility(View.VISIBLE);
                spinnerTolerance.setVisibility(View.VISIBLE);
                spinnerPPM.setVisibility(View.GONE);

                TV1.setVisibility(View.VISIBLE);
                TV2.setVisibility(View.VISIBLE);
                TV3.setVisibility(View.GONE);
                TVALL.setVisibility(View.VISIBLE);
                TVALL.setText(R.string.Color3);
                TVTolerance.setVisibility(View.VISIBLE);
                TVTolerance.setText(R.string.Color4);
                TVPPM.setVisibility(View.GONE);
            } else if (RB2.isChecked()) {
                spinner1.setVisibility(View.VISIBLE);
                spinner2.setVisibility(View.VISIBLE);
                spinner3.setVisibility(View.VISIBLE);
                spinnerALL.setVisibility(View.VISIBLE);
                spinnerTolerance.setVisibility(View.VISIBLE);
                spinnerPPM.setVisibility(View.GONE);

                TV1.setVisibility(View.VISIBLE);
                TV2.setVisibility(View.VISIBLE);
                TV3.setVisibility(View.VISIBLE);
                TVALL.setVisibility(View.VISIBLE);
                TVALL.setText(R.string.Color4);
                TVTolerance.setVisibility(View.VISIBLE);
                TVTolerance.setText(R.string.Color5);
                TVPPM.setVisibility(View.GONE);
            } else if (RB3.isChecked()) {
                spinner1.setVisibility(View.VISIBLE);
                spinner2.setVisibility(View.VISIBLE);
                spinner3.setVisibility(View.VISIBLE);
                spinnerALL.setVisibility(View.VISIBLE);
                spinnerTolerance.setVisibility(View.VISIBLE);
                spinnerPPM.setVisibility(View.VISIBLE);

                TV1.setVisibility(View.VISIBLE);
                TV2.setVisibility(View.VISIBLE);
                TV3.setVisibility(View.VISIBLE);
                TVALL.setVisibility(View.VISIBLE);
                TVALL.setText(R.string.Color4);
                TVTolerance.setVisibility(View.VISIBLE);
                TVTolerance.setText(R.string.Color5);
                TVPPM.setVisibility(View.VISIBLE);
            }

            View view = findViewById(R.id.ViewLine);
            View view2 = findViewById(R.id.ViewLine2);
            view.setVisibility(View.VISIBLE);
            view2.setVisibility(View.VISIBLE);
            GridLayout AllResults = (GridLayout) findViewById(R.id.GridLayoutResults);
            AllResults.setVisibility(View.VISIBLE);

            onCalculate();
        }
    });
}

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

    if (SpinerOhm.getSelectedItem() != getString(R.string.KOhm)) {
        TextoResultado.setVisibility(View.VISIBLE);
        TextoResultadoKOhm.setVisibility(View.GONE);
    } else {
        TextoResultado.setVisibility(View.GONE);
        TextoResultadoKOhm.setVisibility(View.VISIBLE);
    }

    if (adapterView.getItemAtPosition(i).toString() == getString(R.string.Negro)) {
        spinner1.setBackgroundColor(getResources().getColor(R.color.Negro));
        Banda1 = 0;
    } else if (adapterView.getItemAtPosition(i).toString() == getString(R.string.Marron)) {
        spinner1.setBackgroundColor(getResources().getColor(R.color.Marrón));
        Banda1 = 1;
    } else if (adapterView.getItemAtPosition(i).toString() == getString(R.string.Rojo)) {
        spinner1.setBackgroundColor(getResources().getColor(R.color.Rojo));
        Banda1 = 2;
    } else if (adapterView.getItemAtPosition(i).toString() == getString(R.string.Naranja)) {
        spinner1.setBackgroundColor(getResources().getColor(R.color.Naranja));
        Banda1 = 3;
    } else if (adapterView.getItemAtPosition(i).toString() == getString(R.string.Amarillo)) {
        spinner1.setBackgroundColor(getResources().getColor(R.color.Amarillo));
        Banda1 = 4;
    } else if (adapterView.getItemAtPosition(i).toString() == getString(R.string.Verde)) {
        spinner1.setBackgroundColor(getResources().getColor(R.color.Verde));
        Banda1 = 5;
    } else if (adapterView.getItemAtPosition(i).toString() == getString(R.string.Azul)) {
        spinner1.setBackgroundColor(getResources().getColor(R.color.Azul));
        Banda1 = 6;
    } else if (adapterView.getItemAtPosition(i).toString() == getString(R.string.Purpura)) {
        spinner1.setBackgroundColor(getResources().getColor(R.color.Purpura));
        Banda1 = 7;
    } else if (adapterView.getItemAtPosition(i).toString() == getString(R.string.Gris)) {
        spinner1.setBackgroundColor(getResources().getColor(R.color.Gris));
        Banda1 = 8;
    } else if (adapterView.getItemAtPosition(i).toString() == getString(R.string.Blanco)) {
        spinner1.setBackgroundColor(getResources().getColor(R.color.Blanco));
        Banda1 = 9;
    }

    if (spinner2.getSelectedItem() == getString(R.string.Negro)) {
        spinner2.setBackgroundColor(getResources().getColor(R.color.Negro));
        Banda2 = 0;
    } else if (spinner2.getSelectedItem() == getString(R.string.Marron)) {
        spinner2.setBackgroundColor(getResources().getColor(R.color.Marrón));
        Banda2 = 1;
    } else if (spinner2.getSelectedItem() == getString(R.string.Rojo)) {
        spinner2.setBackgroundColor(getResources().getColor(R.color.Rojo));
        Banda2 = 2;
    } else if (spinner2.getSelectedItem() == getString(R.string.Naranja)) {
        spinner2.setBackgroundColor(getResources().getColor(R.color.Naranja));
        Banda2 = 3;
    } else if (spinner2.getSelectedItem() == getString(R.string.Amarillo)) {
        spinner2.setBackgroundColor(getResources().getColor(R.color.Amarillo));
        Banda2 = 4;
    } else if (spinner2.getSelectedItem() == getString(R.string.Verde)) {
        spinner2.setBackgroundColor(getResources().getColor(R.color.Verde));
        Banda2 = 5;
    } else if (spinner2.getSelectedItem() == getString(R.string.Azul)) {
        spinner2.setBackgroundColor(getResources().getColor(R.color.Azul));
        Banda2 = 6;
    } else if (spinner2.getSelectedItem() == getString(R.string.Purpura)) {
        spinner2.setBackgroundColor(getResources().getColor(R.color.Purpura));
        Banda2 = 7;
    } else if (spinner2.getSelectedItem() == getString(R.string.Gris)) {
        spinner2.setBackgroundColor(getResources().getColor(R.color.Gris));
        Banda2 = 8;
    } else if (spinner2.getSelectedItem() == getString(R.string.Blanco)) {
        spinner2.setBackgroundColor(getResources().getColor(R.color.Blanco));
        Banda2 = 9;
    }

    if (spinner3.getSelectedItem() == getString(R.string.Negro)) {
        spinner3.setBackgroundColor(getResources().getColor(R.color.Negro));
        Banda3 = 0;
    } else if (spinner3.getSelectedItem() == getString(R.string.Marron)) {
        spinner3.setBackgroundColor(getResources().getColor(R.color.Marrón));
    } else if (spinner3.getSelectedItem() == getString(R.string.Rojo)) {
        spinner3.setBackgroundColor(getResources().getColor(R.color.Rojo));
        Banda3 = 1;
    } else if (spinner3.getSelectedItem() == getString(R.string.Naranja)) {
        spinner3.setBackgroundColor(getResources().getColor(R.color.Naranja));
        Banda3 = 2;
    } else if (spinner3.getSelectedItem() == getString(R.string.Amarillo)) {
        spinner3.setBackgroundColor(getResources().getColor(R.color.Amarillo));
        Banda3 = 3;
    } else if (spinner3.getSelectedItem() == getString(R.string.Verde)) {
        spinner3.setBackgroundColor(getResources().getColor(R.color.Verde));
        Banda3 = 4;
    } else if (spinner3.getSelectedItem() == getString(R.string.Azul)) {
        spinner3.setBackgroundColor(getResources().getColor(R.color.Azul));
        Banda3 = 5;
    } else if (spinner3.getSelectedItem() == getString(R.string.Purpura)) {
        spinner3.setBackgroundColor(getResources().getColor(R.color.Purpura));
        Banda3 = 6;
    } else if (spinner3.getSelectedItem() == getString(R.string.Gris)) {
        spinner3.setBackgroundColor(getResources().getColor(R.color.Gris));
        Banda3 = 7;
    } else if (spinner3.getSelectedItem() == getString(R.string.Blanco)) {
        spinner3.setBackgroundColor(getResources().getColor(R.color.Blanco));
        Banda3 = 8;
    }

    if (spinnerALL.getSelectedItem() == getString(R.string.Negro)) {
        spinnerALL.setBackgroundColor(getResources().getColor(R.color.Negro));
        Multipler = 1;
    } else if (spinnerALL.getSelectedItem() == getString(R.string.Marron)) {
        spinnerALL.setBackgroundColor(getResources().getColor(R.color.Marrón));
        Multipler = 10;
    } else if (spinnerALL.getSelectedItem() == getString(R.string.Rojo)) {
        spinnerALL.setBackgroundColor(getResources().getColor(R.color.Rojo));
        Multipler = 100;
    } else if (spinnerALL.getSelectedItem() == getString(R.string.Naranja)) {
        spinnerALL.setBackgroundColor(getResources().getColor(R.color.Naranja));
        Multipler = 1000;
    } else if (spinnerALL.getSelectedItem() == getString(R.string.Amarillo)) {
        spinnerALL.setBackgroundColor(getResources().getColor(R.color.Amarillo));
        Multipler = 10000;
    } else if (spinnerALL.getSelectedItem() == getString(R.string.Verde)) {
        spinnerALL.setBackgroundColor(getResources().getColor(R.color.Verde));
        Multipler = 100000;
    } else if (spinnerALL.getSelectedItem() == getString(R.string.Azul)) {
        spinnerALL.setBackgroundColor(getResources().getColor(R.color.Azul));
        Multipler = 1000000;
    } else if (spinnerALL.getSelectedItem() == getString(R.string.Purpura)) {
        spinnerALL.setBackgroundColor(getResources().getColor(R.color.Purpura));
        Multipler = 10000000;
    } else if (spinnerALL.getSelectedItem() == getString(R.string.Gris)) {
        spinnerALL.setBackgroundColor(getResources().getColor(R.color.Gris));
        Multipler = 100000000;
    } else if (spinnerALL.getSelectedItem() == getString(R.string.Blanco)) {
        spinnerALL.setBackgroundColor(getResources().getColor(R.color.Blanco));
        Multipler = 1000000000;
    } else if (spinnerALL.getSelectedItem() == getString(R.string.Dorado)) {
        spinnerALL.setBackgroundColor(getResources().getColor(R.color.Dorado));
        Divider = 10;
    } else if (spinnerALL.getSelectedItem() == getString(R.string.Plateado)) {
        spinnerALL.setBackgroundColor(getResources().getColor(R.color.Plateado));
        Divider = 100;
    }

    if (spinnerTolerance.getSelectedItem() == getString(R.string.Marron)) {
        spinnerTolerance.setBackgroundColor(getResources().getColor(R.color.Marrón));
        Tolerance = "±1%";
    } else if (spinnerTolerance.getSelectedItem() == getString(R.string.Rojo)) {
        spinnerTolerance.setBackgroundColor(getResources().getColor(R.color.Rojo));
        Tolerance = "±2%";
    } else if (spinnerTolerance.getSelectedItem() == getString(R.string.Dorado)) {
        spinnerTolerance.setBackgroundColor(getResources().getColor(R.color.Dorado));
        Tolerance = "±5%";
    } else if (spinnerTolerance.getSelectedItem() == getString(R.string.Plateado)) {
        spinnerTolerance.setBackgroundColor(getResources().getColor(R.color.Plateado));
        Tolerance = "±10%";
    }


    if (spinnerPPM.getSelectedItem() == getString(R.string.Marron)) {
        spinnerPPM.setBackgroundColor(getResources().getColor(R.color.Marrón));
        PPM = "100 PPM";
    } else if (spinnerPPM.getSelectedItem() == getString(R.string.Rojo)) {
        spinnerPPM.setBackgroundColor(getResources().getColor(R.color.Rojo));
        PPM = "50 PPM";
    } else if (spinnerPPM.getSelectedItem() == getString(R.string.Naranja)) {
        spinnerPPM.setBackgroundColor(getResources().getColor(R.color.Naranja));
        PPM = "15 PPM";
    } else if (spinnerPPM.getSelectedItem() == getString(R.string.Amarillo)) {
        spinnerPPM.setBackgroundColor(getResources().getColor(R.color.Amarillo));
        PPM = "25 PPM";
    } else if (spinnerPPM.getSelectedItem() == getString(R.string.Azul)) {
        spinnerPPM.setBackgroundColor(getResources().getColor(R.color.Azul));
        PPM = "10 PPM";
    } else if (spinnerPPM.getSelectedItem() == getString(R.string.Purpura)) {
        spinnerPPM.setBackgroundColor(getResources().getColor(R.color.Purpura));
        PPM = "5 PPM";
    } else if (spinnerPPM.getSelectedItem() == getString(R.string.Blanco)) {
        spinnerPPM.setBackgroundColor(getResources().getColor(R.color.Blanco));
        PPM = "1 PPM";
    }

    onCalculate();
    if (IntersticialBoolean) {
        if (AnuncioIntersticial.isLoaded()) {
            AnuncioIntersticial.show();
        }
    }
}

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

}

public void onCalculate() {
    if (RB1.isChecked()) {

        Union = String.valueOf(Banda1) + String.valueOf(Banda2);
        if (spinnerALL.getSelectedItem() == getString(R.string.Dorado) || spinnerALL.getSelectedItem() == getString(R.string.Plateado)) {
            T = Integer.parseInt(Union) / Divider;
        } else {
            T = Integer.parseInt(Union) * Multipler;
        }
        TextoResultado.setText(String.valueOf(T) + " Ω  " + Tolerance);
        TextoResultadoKOhm.setText(String.valueOf(Float.parseFloat(String.valueOf(T)) / 1000) + " KΩ  " + Tolerance);

    } else if (RB2.isChecked()) {

        Union = String.valueOf(Banda1) + String.valueOf(Banda2) + String.valueOf(Banda3);
        if (spinnerALL.getSelectedItem() == getString(R.string.Dorado) || spinnerALL.getSelectedItem() == getString(R.string.Plateado)) {
            T = Integer.parseInt(Union) / Divider;
        } else {
            T = Integer.parseInt(Union) * Multipler;
        }
        TextoResultado.setText(String.valueOf(T) + " Ω  " + Tolerance);
        TextoResultadoKOhm.setText(String.valueOf(Float.parseFloat(String.valueOf(T)) / 1000) + " KΩ  " + Tolerance);

    } else if (RB3.isChecked()) {
        Union = String.valueOf(Banda1) + String.valueOf(Banda2) + String.valueOf(Banda3);
        if (spinnerALL.getSelectedItem() == getString(R.string.Dorado) || spinnerALL.getSelectedItem() == getString(R.string.Plateado)) {
            T = Integer.parseInt(Union) / Divider;
        } else {
            T = Integer.parseInt(Union) * Multipler;
        }
        TextoResultado.setText(String.valueOf(T) + " Ω  " + Tolerance + "  " + PPM);
        TextoResultadoKOhm.setText(String.valueOf(Float.parseFloat(String.valueOf(T)) / 1000) + " KΩ  " + Tolerance + "  " + PPM);
    }
}

public void PublicidadIntersticial() {
    AnuncioIntersticial = new InterstitialAd(this);
    AnuncioIntersticial.setAdUnitId(adId);
    AnuncioIntersticial.setAdListener(new AdListener() {

        @Override
        public void onAdClosed() {
            IntersticialBoolean = false;
        }

    });

    AdRequest adRequest = new AdRequest.Builder().build();
    AnuncioIntersticial.loadAd(adRequest);
}

}

    
asked by Juan David Rueda Quiroga 03.12.2017 в 05:43
source

1 answer

-2

The correct way to define the RadioButton that is being activated within the RadioGroup , is by its id, this id can be obtained by means of the second parameter of the method onCheckedChanged() :

public void onCheckedChanged(RadioGroup radioGroup, int idCheck) 

Therefore to know what is the RadioButton selected according to your code, it will be done in this way:

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            if (checkedId == R.id.RB1) {
             ...
             ...
             ...
            } else  if (checkedId == R.id.RB2) {
             ...
             ...
             ...
            } else  if (checkedId == R.id.RB3) {
             ...
             ...
             ... 
            }

        }
    });

Ensure that the used import is:

import android.widget.RadioGroup;

and that the RadioButtons are actually within the RadioGroup :

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/text"
    android:id="@+id/RG">

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/RB1"/>

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/RB2"/>

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/RB3"/>

</RadioGroup>

I add a example to check the behavior.

    
answered by 03.12.2017 в 06:23