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(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String item = priority.getSelectedItem().toString();
// do checking condition here
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Solution 2:
Try implement this in your code:
priority.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View arg1, int position, long arg3)
{
// TODO Auto-generated method stub
if(parent.getItemAtPosition(position).toString().equals("Low"))
imageId = R.drawable.blue;
else if(parent.getItemAtPosition(position).toString().equals("Medium")) // you can use equalsIgnoreCase if you want.
imageId = R.drawable.green;
else
imageId = R.drawable.red;
}
@Override
public void onNothingSelected(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;
else if(priortyStr.equals("Medium"))
imageId = R.drawable.green;
else
imageId = R.drawable.red;
Post a Comment for "If Condition Does Not Work In Android Spinner"