Spinner 忽略条件语句
所以我得到了带有 StringArray 条目的微调器。
String[] spinnerEntries = {
getResources().getString(R.string.oldPoint),
getResources().getString(R.string.newPoint),
getResources().getString(R.string.gpsLocation) };
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, spinnerEntries);
我想根据微调器选择设置 searchPoint
,但它完全忽略除第一个(oldPoint)之外的所有情况。我做错了什么?不管我是太累还是太傻,但我就是看不出问题所在。
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String spinnerItem = (String) parent.getItemAtPosition(pos);
if (spinnerItem.equals(getString(R.string.oldPoint))) {
searchPoint = R.string.oldPoint;
} else if (spinnerItem.equals(R.string.newPoint)) {
searchPoint = R.string.newPoint;
} else if (spinnerItem.equals(R.string.gpsLocation)) {
searchPoint = R.string.gpsLocation;
}
}
So i got my spinner with entries from a StringArray.
String[] spinnerEntries = {
getResources().getString(R.string.oldPoint),
getResources().getString(R.string.newPoint),
getResources().getString(R.string.gpsLocation) };
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, spinnerEntries);
I want to set the searchPoint
depending on spinner selection, but it totally igonores all cases except for the first one (oldPoint). What am I doing wrong? Wheter i got too tired or too stupid, but i just don't see the problem.
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String spinnerItem = (String) parent.getItemAtPosition(pos);
if (spinnerItem.equals(getString(R.string.oldPoint))) {
searchPoint = R.string.oldPoint;
} else if (spinnerItem.equals(R.string.newPoint)) {
searchPoint = R.string.newPoint;
} else if (spinnerItem.equals(R.string.gpsLocation)) {
searchPoint = R.string.gpsLocation;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在第一个条件中,您正确地对常量名称调用了
getString
()。然而,在第二个 else 块上,你不是。In the first conditional, you're correctly calling
getString
() on the constant name. However, on the second two else blocks, you're not.