有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我是Android新手,尝试将文本设置为RadioButons。我使用Hashmap方法从列表字符串(Hashmap)中获取值。我可以在日志视图中显示该值,但无法在单选按钮上显示文本

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

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

提前谢谢


共 (1) 个答案

  1. # 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);
    }