在 Java 中使用微调器中的单选按钮
我正在尝试在微调器中添加一些单选按钮。不过我想只使用 java 来做到这一点。我见过很多例子展示如何使用 xml 资源来做到这一点,但没有像这样的。由于某种原因,从数组获取资源的方法对我不起作用。这是我的代码
spinnerrg = new Spinner(con);
NodeList rl = child.getChildNodes();
String[] radioadapt = new String[rl.getLength()];
for(int j=1;j<rl.getLength();j++) {
Element[] dumrad = new Element[rl.getLength()];
Node dum = rl.item(j);
if(dum instanceof Element) {
dumrad[j] = (Element) dum;
radioadapt[j] = dumrad[j].getAttribute("value");
}
}
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(con, android.R.layout.simple_spinner_item, radioadapt);
spinnerrg.setAdapter(adapter);
TextView radioText = new TextView(con);
radioText.setText(child.getAttribute("label"));
spinnerrg.setTag(child.getAttribute("id"));
contList.add(radioText);
contList.add(spinnerrg);
,nodeList 在那里是因为我正在解析 xml 文件。如果我只是使用 radiogroup 但无法使用 spinner 来实现,则 for-loop 可以完美地工作。
I'm trying to add some radiobuttons in a spinner. However I want to do this by using only java. I've seen a lot of examples showing how to do it with xml resource but nothing like this. Ones getting resource from an array didn't work for me for some reason. Here's my code
spinnerrg = new Spinner(con);
NodeList rl = child.getChildNodes();
String[] radioadapt = new String[rl.getLength()];
for(int j=1;j<rl.getLength();j++) {
Element[] dumrad = new Element[rl.getLength()];
Node dum = rl.item(j);
if(dum instanceof Element) {
dumrad[j] = (Element) dum;
radioadapt[j] = dumrad[j].getAttribute("value");
}
}
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(con, android.R.layout.simple_spinner_item, radioadapt);
spinnerrg.setAdapter(adapter);
TextView radioText = new TextView(con);
radioText.setText(child.getAttribute("label"));
spinnerrg.setTag(child.getAttribute("id"));
contList.add(radioText);
contList.add(spinnerrg);
That nodeList is there because I'm parsing an xml file. for-loop works perfectly if I simply use radiogroup but can't manage to do it with spinner.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看这个
Look at this