2012-05-12 7 views
3

私はTableLayoutを持っていて、ラジオ・グループを配置するすべての行の3番目の列にあります。 私はこのようなラジオボタンを構築:ラジオ・グループにラジオ・ボタンを追加する方法

rg = (RadioGroup) findViewById(R.id.radioGroup1); 

for (int k = 0; k < size; k++) { 
    rb[k] = new RadioButton(context); 
    rg.addView(rb[k]); 
} 

しかし、これは、私のアプリがクラッシュする任意のアイデア?

+0

いただきました、あなたが直面している正確な問題を拡張しますか?ラジオグループをテーブルレイアウトに追加するかラジオボタンにラジオボタンを追加しますか? –

+0

2番目のラジオボタンはOKです。私はそれらをグループにすることはできません。そのため、一度に1つだけ選択することができます。いつも、「rg.addView(rb [k]);」を追加します。クラッシュします。 –

+0

'rb'を定義する場所に投稿してください。 – Sam

答えて

4

長さがmegethosのプリミティブ配列を作成していますが、ループの長さはsizeです。 megethossizeが異なる値の場合、さまざまなタイプのエラーが発生する可能性があります...しかし、これはRadioGroupがこの配列を最新の状態に保ちますので、冗長です。

私はこのような何かしようとするだろう。また、常にあなたのlogcatエラーを投稿してください、それは正確を教えてくれる

group.getChildAt(index); 

RadioGroup group = (RadioGroup) findViewById(R.id.radioGroup1); 
RadioButton button; 
for(int i = 0; i < 3; i++) { 
    button = new RadioButton(this); 
    button.setText("Button " + i); 
    group.addView(button); 
} 

そして、あなたはindexでボタンを参照したいです何が間違っていて、どこを見ていたのか。それ以外の場合は、このように推測する必要があります。

更新

使用すると、2つの異なるレイアウトに同じボタンを追加しようとしているため、エラーがある:

tr[k].addView(rb[k]); 
rg.addView(rb[k]); 

ビューは一つだけの親を持つことができます。私が知る限り、最初に多くのカスタマイズをしなくても、RadioGroupを複数のビューに分割することはできません。しかしRADIOGROUPのように振る舞い、すでに組み込まれている機能のsetChoiceModeリストビュー():

List<String> list = new ArrayList<String>(); 
list.add("one"); 
list.add("two"); 
list.add("three"); 

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, list); 
ListView listView = (ListView) findViewById(R.id.list); 
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
listView.setAdapter(adapter); 

あなたは簡単にSSIDと信号強度を表示するためにsimple_list_item_checkedを適応させることができます。希望が役立ちます。 (もしあなたが私の答えをグラフィカルな変更で貼り付けることができたら、&は自分の答えを貼り付けることができるかもしれません。)

+0

http://hostcode.sourceforge.net/view/548 thatsコード thatsエラー http://hostcode.sourceforge .net/view/548 –

+0

あなたは何をしようとしているのですか。しかしそれらは同じリンクです、あなたはあなたのlogcatエラーへのリンクを再投稿できますか? – Sam

+0

http://i47.tinypic.com/343pzph。jpg 私は混乱のために申し訳ありません:$ –

0

私はラジオボタンを動的に追加し、 。

パブリッククラスMainActivityはAppCompatActivity {

RadioGroup radioGroup2; 
ArrayList<String> buttonNames; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    radioGroup2 = (RadioGroup) findViewById(R.id.radioGroup2); 
    buttonNames = new ArrayList<>(); 
    buttonNames.add("No Tip"); 
    buttonNames.add("10%"); 
    buttonNames.add("20%"); 
    buttonNames.add("30%"); 
    radioGroup2.setWeightSum(Float.parseFloat(buttonNames.size() + "")); 
    radioGroup2.setBackgroundColor(Color.BLUE); 


    for (int i = 0; i < buttonNames.size(); ++i) { 

     RadioButton radioButton = new RadioButton(this); 
     radioButton.setId(i); 
     RadioGroup.LayoutParams childParam1 = new RadioGroup.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1f); 
     childParam1.setMarginEnd(2); 
     radioButton.setGravity(Gravity.CENTER); 
     radioButton.setLayoutParams(childParam1); 
     radioButton.setBackground(null); 
     radioButton.setText(buttonNames.get(i)); 
     radioButton.setTextColor(Color.BLUE); 
     radioButton.setBackgroundColor(Color.WHITE); 
     radioButton.setButtonDrawable(null); 
     if (buttonNames.get(i).equals("20%")) { 
      radioButton.setChecked(true); 
      radioButton.setBackgroundColor(Color.BLUE); 
      radioButton.setTextColor(Color.WHITE); 
     } 
     radioGroup2.addView(radioButton, childParam1); 

    } 


    radioGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(RadioGroup radioGroup, int i1) { 
      RadioButton button = null; 

      for (int i = 0; i < buttonNames.size(); ++i) { 
       if (i1 == i) { 
        button = (RadioButton) findViewById(i1); 
        button.setChecked(true); 
        button.setBackgroundColor(Color.BLUE); 
        button.setTextColor(Color.WHITE); 
        Toast.makeText(getApplicationContext(), button.getText() + " checked", Toast.LENGTH_SHORT).show(); 
       } else { 
        button = (RadioButton) findViewById(i); 
        button.setChecked(false); 
        button.setBackgroundColor(Color.WHITE); 
        button.setTextColor(Color.BLUE); 
       } 
      } 
     } 
    }); 


} 

}

+0

レイアウトファイルは: –

関連する問題