gpt4 book ai didi

java - 如何将 HashMap 的值映射到单选按钮

转载 作者:行者123 更新时间:2023-12-01 12:00:00 25 4
gpt4 key购买 nike

我是 Android 新手,尝试将 Text 设置到 RadioButons 上。我正在使用 Hashmap 方法从列表字符串(HashMap)中获取值。我能够在日志 View 中显示值,但无法将文本获取到单选按钮

请检查代码并提出解决方案

RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
radioGroup = new RadioGroup(this);

final RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);

for (Map.Entry<String, List<String>> entry : map.entrySet()) {
key = entry.getKey();
values = entry.getValue() != null;
System.out.println("Key = " + key);
System.out.println("Values = " + values + "n");
RadioButton radioButtonView = new RadioButton(this);
radioButtonView.setFreezesText(values);
radioGroup.addView(radioButtonView, p);

}

提前致谢

最佳答案

使用这段代码可能会对您有所帮助

final   Hashtable<String, String> ht = new Hashtable<String, String>(); 
ht.put("player 1", "sachin");
ht.put("player 2", "sehwag");
ht.put("player 3", "dhoni");
final Enumeration<String> values = ht.keys();

while (values.hasMoreElements()) {
String str = (String) values.nextElement();
System.out.println(str + ":" + ht.get(str));

RadioButton radioButtonView = new RadioButton(this);
radioButtonView.setText(ht.get(str));
selectedId = radioGroup.getCheckedRadioButtonId();
radioGroup.addView(radioButtonView, p);
}

关于java - 如何将 HashMap 的值映射到单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28045321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com