Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Spinner validation

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?


2 Answers

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();
}
like image 165
Chirag Ghori Avatar answered Mar 23 '26 12:03

Chirag Ghori


Simply use this.

else if (Spinner1.getSelectedItem().toString().trim().equalsIgnoreCase("Pick one")) {
            Toast.makeText(CallWs.this, "Error",
                    Toast.LENGTH_SHORT).show();
like image 21
Piyush Avatar answered Mar 23 '26 10:03

Piyush



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!