[英]Is there any way to programmatically set the selection of a spinner by it's name?
I've been trying to figure this one out for a while now, but no matter how much I look, I haven't found an answer. 我已经尝试了好一阵子了,但是无论我看起来多少,都没有找到答案。 So far I've checked the following questions: Set selected item of spinner programmatically , How to set selected item of Spinner by value, not by position?
到目前为止,我已经检查了以下问题: 以编程 方式设置 微调框的选定项 , 如何通过值而不是位置来设置微调框的选定项? , and How to set position in spinner?
,以及如何在微调器中设置位置? .
。 So far none of them have worked for me.
到目前为止,他们都没有为我工作。 I'm trying to do this on Android 4.4.2, and here's my code:
我正在尝试在Android 4.4.2上执行此操作,这是我的代码:
String dataReturned11 = someData.getString("unitCheckFour", "degrees F");
if (dataReturned11 == ("degrees C")) {
tempUnits.setSelection(((ArrayAdapter<String>)tempUnits.getAdapter()).getPosition("degrees C"));
} else {
tempUnits.setSelection(((ArrayAdapter<String>)tempUnits.getAdapter()).getPosition("degrees F"));
};
What I'm trying to do is set it based on a value that was saved earlier, and it doesn't seem to be working. 我要尝试做的是基于先前保存的值进行设置,但似乎没有用。 An important thing to note is that this gave me an unchecked cast warning, so I suppressed it.
需要注意的重要一点是,这给了我不受限制的强制警告,因此我禁止了它。 It does not seem to have had any effect on my problem whatsoever.
它似乎对我的问题没有任何影响。
EDIT: Since it was asked for, here is the code behind the spinner itself: 编辑:由于被要求,这是微调器本身背后的代码:
tempUnits = (Spinner) findViewById(R.id.tempUnits);
ArrayAdapter<CharSequence> adapter4 = ArrayAdapter.createFromResource(this,
R.array.tempUnits, android.R.layout.simple_spinner_item);
adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
tempUnits.setAdapter(adapter4);
I hope this helps. 我希望这有帮助。
yes you can . 是的你可以 。
for (int i=0;i<projectArray.size();i++)
{
if (projectArray.get(i).getName().equals(tasksArray.get(position).getProject()))
{
project.setSelection(i);
return;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.