【问题标题】:how to map value of HashMap onto RadioButtons如何将 HashMap 的值映射到单选按钮
【发布时间】:2018-06-22 15:36:04
【问题描述】:

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

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

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);
    	            
   }

提前致谢

【问题讨论】:

  • 检查这个例子stackoverflow.com/questions/6426317/…。它可以帮助你
  • 本教程将帮助java2s.com/Code/Android/UI/…
  • 我正在检查两个链接谢谢@Swarna
  • 我正在检查链接@Ichigo Kurosaki
  • @Swarna 我可以在以前的项目中制作 RadioButtons 文本,但现在要求是 HashMap,我可以将值输入日志但不能在 RadioButtons 上

标签: java android hashmap radio-button


【解决方案1】:

这可能对你有帮助,使用这段代码

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);
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 2012-12-26
    • 2019-09-13
    相关资源
    最近更新 更多