微调如果字符串** ==不工作 [英] spinner If Strings** == not working
问题描述
这是为什么不工作?!
if(itemx == "Test number item 0")
{
Log.i("Dropdown", "inside if");
us_lo_ans_hold.setText("0x");
};
如果itemx是一个字符串,它有字符串测试号项目0'。
if itemx is a String and it has the string 'Test number item 0'.
我创建我的第一次微调。
I am creating my first spinner.
下面是活动的OnCreate块内的微调code:
Here is the spinner code within the onCreate block of the Activity:
Spinner us_lo_spinner = (Spinner) findViewById(R.id.ul_ans_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.test_defaults , android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
us_lo_spinner.setAdapter(adapter);
// Spinner click listener
us_lo_spinner.setOnItemSelectedListener(this);
下面是在strings.xml中的String数组:
Here is the String array in the strings.xml:
<string name="spin_title_ulover_ans">Spinner default:</string>
<string-array name="test_defaults">
<item>Test number item 0</item>
<item>Test number item 1</item>
<item>Test number item 2</item>
<item>Test number item 3</item>
<item>Test number item 4</item>
</string-array>
下面是onItemSelected:
Here is the onItemSelected:
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
Log.i("XXX_Dropdown", "XXXX");
TextView us_lo_ans_hold = (TextView)findViewById(R.id.us_lo_ans_holdtest);
// On selecting a spinner item
String itemx = arg0.getSelectedItem().toString();
us_lo_ans_hold.setText("oppp12");
Log.i("Dropdown", "item: " + itemx);
Log.i("Dropdown", "arg1: " + arg1);
Log.i("Dropdown", "arg2: " + arg2);
Log.i("Dropdown", "arg3: " + arg3);
// Showing selected spinner item
if(itemx == "Test number item 0")
{
Log.i("Dropdown", "inside if");
us_lo_ans_hold.setText("0x");
};
}
这是所有Log.i(,)■我把我能告诉itemx是正确的,它具有正确的字符串时,在微调的项目被选中。 然而,如果将无法正常工作。
From all the Log.i(,)s I put in I can tell the itemx is correct, it has the correct string when an item in the spinner is selected. Yet the IF will not work.
如果我将如果以这样的:
If I change the IF to this:
if(arg2 == 0)
{
Log.i("Dropdown", "inside if");
us_lo_ans_hold.setText("0x");
};
将工作;但是,这不是我想要的!
it will work; BUT that is not what I want!
所以......这是为什么不工作?!
So... why is this not working ?!:
if(itemx == "Test number item 0")
{
Log.i("Dropdown", "inside if");
us_lo_ans_hold.setText("0x");
};
如果itemx是一个字符串,它有字符串测试数项0。 ?
if itemx is a String and it has the string 'Test number item 0'. ?
还如何或何时会onNothingSelected被称为? 当你点击后退按钮? 当您选择该项目已被选中? 我的onNothingSelected永远不会被调用!
also how or when would 'onNothingSelected' be called? When you hit the back button ? When you select the item already selected? My 'onNothingSelected' is never called !
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
Log.i("Dropdown", "in NOTHING SELECTED");
TextView us_lo_ans_hold = (TextView)findViewById(R.id.us_lo_ans_holdtest);
us_lo_ans_hold.setText("nothing selected");
}
试图把在这样的活动的onCreate块:
Tried putting in the onCreate block of the Activity like the:
// Spinner click listener
us_lo_spinner.setOnItemSelectedListener(this);
us_lo_spinner.onNothingSelected(this)
但〜编译〜确实如此。
but the ~compiler~ does like that.
所以...如何或何时被onNothingSelected打来电话,它是如何迷上了一个微调?
So... how or when is 'onNothingSelected' called and how is it hooked up to a spinner ?
推荐答案
如果你的 itemx
是一个变量,那么你不能比较两个字符串 = =
。使用而不是 items.equals(测试号项目0);
If your itemx
is a variable then you cant compare two strings with ==
. Use instead items.equals("Test number item 0");
==
比较的对象和等于()
比较两个字符串值。
看看一本帖子。
==
compares objects and equals()
compare string values.
Take a look a this post.
这篇关于微调如果字符串** ==不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!