mercredi 22 avril 2015

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 details are stored in the sqlite database.

Here is the sample image of the UI:

enter image description here

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);";

Even with Spinner Listener, it doesn't seem to work. Here is the code:

priority.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    if(priority.getSelectedItem().toString().equalsIgnoreCase("low"))
                        imageId = R.drawable.blue;
                    else if(priority.getSelectedItem().toString().equalsIgnoreCase("medium"))
                        imageId = R.drawable.green;
                    else
                        imageId = R.drawable.red;
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });

And the values from the log for the images,

04-22 06:42:01.414    2418-2418/com.app D/imageId﹕ 0
04-22 06:42:01.416    2418-2418/com.app D/red﹕ 2130837558
04-22 06:42:01.416    2418-2418/com.app D/blue﹕ 2130837555
04-22 06:54:30.795    1942-1942/com.app D/priority﹕ Low

The imageId I get is 0, but I am supposed to get one of the above values. The D/priority is the value from the spinner (priority.getSelectedItem().toString()). Where I am going wrong?

Aucun commentaire:

Enregistrer un commentaire