我正面临一个问题,即if条件未在android中验证。
我有一个UI(与之前的问题相同的用户界面),当用户点击保存按钮时,详细信息存储在sqlite数据库中。
以下是用户界面的示例图片:
对于每项任务,我都会根据我在drawable
文件夹中存储的优先级关联图像。
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);
插入数据库后,我切换到另一个活动,其中详细信息显示为列表。
这个特殊代码:
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());
,我想检查是否正确选择了优先级,它显示低但是给出了红色,它不应该是。我在做错的地方?
equals()
和equalsIgnoreCase()
似乎都不起作用。即使==
似乎也不起作用(我知道使用==
比较字符串是错误的,但我想尝试一下)。
供参考: 我用来创建表的语句如下:
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);";
即使使用Spinner Listener,它似乎也无法工作。这是代码:
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是微调器(priority.getSelectedItem().toString()
)的值。我哪里错了?
答案 0 :(得分:0)
首先像这样添加Spinner的监听器
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
}
});
答案 1 :(得分:0)
尝试在您的代码中实现此功能:
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
}
});
希望这能以某种方式帮助你。
答案 2 :(得分:0)
终于找到了解决方案:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<button id="clonerow">Clone Row</button>
<button id="clonecolumn">Clone Column</button>
<table id="tableID">
<tr>
<th>one</th><th>two</th>
</tr>
<tr>
<td>1</td><td data-foo="metadata">2</td>
</tr>
<tr>
<td>3</td><td>4</td>
</tr>
<tr>
<td>5</td><td>6</td>
</tr>
</table>