user3872646 user3872646 -3 years ago 134
Android Question

selected item of a spinner in an if statement

How can I use if statement when the selected value = 0 ?

if( wilayat.setSelection(0) || city.setSelection(0) ||


station.setSelection(0) || distnation.setSelection(0)) {

message = "Select the 4 fields, please..";
Toast.makeText(BusArrivalTime.this, message, Toast.LENGTH_SHORT).show();

}

Answer Source

Try this.

if (wilayat.getSelectedItemPosition() == 0 || city.getSelectedItemPosition() == 0 || station.getSelectedItemPosition() == 0 || distnation.getSelectedItemPosition() == 0) {
        message = "Select the 4 fields, please..";
        Toast.makeText(BusArrivalTime.this, message, Toast.LENGTH_SHORT).show();
}

Just use getSelectedItemPosition() method.

You make sure that wilayatcity and so on were Spinner.

Add judgement

if (wilayat != null || city != null || station != null || distnation != null) {
    // do something
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download