如果条件不Android的微调工作 [英] If condition does not work in Android Spinner

查看:111
本文介绍了如果条件不Android的微调工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对着那里的,如果条件没有在android系统验证的一个问题。

I am facing an issue where the if condition is not validated in android.

我有一个UI(相同的用户界面为previous问题),其中,当用户点击保存按钮的详细信息存储在SQLite数据库中

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.

这个特殊的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;

不起作用。因为我得到一个deafult红色图像的所有任务,不论优先。在这里, Log.d(优先权,priority.getSelectedItem()的toString()); ,我想检查是否选择了正确的优先级,它显示的反而给人一种红色,它不应该。在那里我做了错误?

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?

两者等于() equalsIgnoreCase()好像不工作。即使 == 似乎并没有工作(我知道这是错误的使用 == 来比较字符串,但我想试试看)。

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

即使有微调监听器,它似乎并没有工作。这里是code:

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) {

                }
            });

和从日志中的图像值,

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

我得到的imageId是0,但我应该得到上面的值之一。在D /优先级是微调值( priority.getSelectedItem()。的toString())。我要去的地方错了?

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?

推荐答案

终于找到了解决办法:

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;

这篇关于如果条件不Android的微调工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆