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 details are stored in the sqlite database.
Here is the sample image of the UI:
For every task, I have an image associated based on the priority which I store in the drawable
folder.
EditText sd = (EditText)findViewById(R.id.sd);
EditText desc = (EditText)findViewById(R.id.description);
Spinner type = (Spinner)findViewById(R.id.type);
Spinner priority = (Spinner)findViewById(R.id.priority);
int imageId = 0;
if(priority.getSelectedItem().toString().equals("Low"))
imageId = R.drawable.blue;
else if(priority.getSelectedItem().toString().equalsIgnoreCase("medium"))
imageId = R.drawable.green;
else
imageId = R.drawable.red;
Log.d("priority",priority.getSelectedItem().toString());
String query = "insert into tasks values(null,'"+sd.getText()+"','"+type.getSelectedItem().toString()+"','"+priority.getSelectedItem().toString()+"','"+desc.getText()+"',CURRENT_DATE,"+imageId+");";
db.execSQL(query);
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
After inserting into the database, I switchover to another activity, where the details are displayed as a list.
This particular code:
if(priority.getSelectedItem().toString().equals("Low"))
imageId = R.drawable.blue;
else if(priority.getSelectedItem().toString().equalsIgnoreCase("medium"))
imageId = R.drawable.green;
else
imageId = R.drawable.red;
doesn't work. Because I get a deafult red image for all the tasks irrespective of the priority. Here Log.d("priority",priority.getSelectedItem().toString());
, I wanted to check if the priority is selected properly, it displays Low but gives a red color which it shouldn't. Where I am doing the mistake?
Both equals()
and equalsIgnoreCase()
don't seem to work. Even ==
doesn't seem to work (I know it is wrong to use ==
to compare strings, but I wanted to give it a try).
For reference: The statement I used to create the table is as follows:
String query = "create table if not exists tasks (id integer primary key autoincrement, shortdesc varchar not null, type varchar not null, priority varchar not null, desc varchar not null, taskdate date not null, imageid int not null);";
Aucun commentaire:
Enregistrer un commentaire