If condition does not work in Android Spinner

拈花ヽ惹草 提交于 2020-01-25 23:09:45

问题


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

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?


回答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

    }
});



回答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.




回答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;


来源:https://stackoverflow.com/questions/29788579/if-condition-does-not-work-in-android-spinner

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!