I need to validate selected item of Spinner in Android.
I tried the following code, but it's not working.
if (Spinner1.getSelectedItem().toString().trim() == "Pick one") {
Toast.makeText(CallWs.this, "Error", Toast.LENGTH_SHORT).show();
}
What is wrong with the code, and how can i fix it?
Use .equals or .equalsIgnoreCase to compare two strings in java/android instead of ==.
Try this
if (Spinner1.getSelectedItem().toString().trim().equals("Pick one")) {
Toast.makeText(CallWs.this, "Error", Toast.LENGTH_SHORT).show();
}
Simply use this.
else if (Spinner1.getSelectedItem().toString().trim().equalsIgnoreCase("Pick one")) {
Toast.makeText(CallWs.this, "Error",
Toast.LENGTH_SHORT).show();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With