Android微调器位置和if语句 [英] Android spinner position and if statements
问题描述
这里有一个奇怪的问题而且不知道为什么它不起作用,我还不习惯java。
确定所选项目需要做什么?
微调器有8个项目,'position'从不= 1,或任何其他数字。
onItemSelected()
肯定会被解雇,所以if语句错了吗?
Having an odd issue here and don't know why it doesn't work, I'm not that used to java yet.
to determine the selected item what needs to be done?
the spinner has 8 items and 'position' never = 1, or any other number.
onItemSelected()
is definitely getting fired so is the if statement wrong?
public void onItemSelected(AdapterView parent, View v,int position, long id) {
if (position == 1) //do something
}
编辑:
谢谢Lion
结果表明位置似乎没有做任何事情。但这样做有效。
thanks Lion it turns out position doesnt seem to do anything. however, this works.
String s = parent.getSelectedItem().toString();
if (s == "1")//do something
推荐答案
你必须为微调器实现适当的监听器。
You have to implement the proper listener for the spinner.
yourSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 1){
//Do something}
}
}
@Override
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
});
如果你不习惯Java,我建议你阅读一些基础知识。
If you're "not that used to Java" I recommend you read some basics.
这篇关于Android微调器位置和if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!