Skip to content Skip to sidebar Skip to footer

If Condition Does Not Work In Android Spinner

I am facing an issue where the if condition is not validated in android. I have an UI (the same UI as in previous questions) where in when the user clicks the save button the detai

Solution 1:

First add listeners for Spinner like this

priority.setOnItemSelectedListener(newOnItemSelectedListener() {
    @OverridepublicvoidonItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
        Stringitem= priority.getSelectedItem().toString();
        // do checking condition here
    }
    @OverridepublicvoidonNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
});

Solution 2:

Try implement this in your code:

      priority.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
        {
            @Override
            publicvoidonItemSelected(AdapterView<?> parent, View arg1, int position, long arg3) 
            {
                // TODO Auto-generated method stubif(parent.getItemAtPosition(position).toString().equals("Low"))
                     imageId = R.drawable.blue;
                elseif(parent.getItemAtPosition(position).toString().equals("Medium")) // you can use equalsIgnoreCase if you want.
                    imageId = R.drawable.green;
                else
                    imageId = R.drawable.red;
            }

            @Override
            publicvoidonNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }
        });

Hope this helps you somehow.

Solution 3:

Finally found the solution :

String priortyStr = priority.getSelectedItem().toString();
int imageId = 0;
if(priortyStr.equals("Low"))
    imageId = R.drawable.blue;
elseif(priortyStr.equals("Medium"))
    imageId = R.drawable.green;
else
    imageId = R.drawable.red;

Post a Comment for "If Condition Does Not Work In Android Spinner"