简体   繁体   English

有什么办法可以通过编程方式设置微调器的名称?

[英]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.

相关问题 选择第一个时,第二个微调器的位置设置为默认值 - Second spinner's position set to default when selection of first changes 有什么方法可以通过编程方式检索已部署工件的名称? - Is there any way to retrieve the name of the deployed artifact programmatically? 如果在JVM上设置了-noverify,是否可以通过编程方式进行检查? - Is there any way to programmatically check, if `-noverify` is set on the JVM? 无法使用对象设置微调器选择值 - Unable to set Spinner selection value with object 适配器未获得 boolean 值来设置微调器选择 - Adapter not getting boolean values to set spinner selection Android:如何用按钮更改微调器的选择? - Android: How to change spinner's selection with a button? 以编程方式修改微调框的下拉框 - Modify spinner's dropdown box programmatically 有什么方法可以隐藏 JavaFX 上微调器的文本字段? - Is there any way of hiding the text field of a spinner on JavaFX? 添加JavaScript时有什么方法可以手动设置“脚本名称”? - any way to manually set the “Script Name” when adding JavaScript? 如何以编程方式在jTree子级上设置选择 - How to programmatically set selection on a jTree child
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM