2011-12-05 9 views
0

"button2.setOnClickListener(radio_listener);"アンドロイドラジオグループの選択に基づいてどのようにラジオグループを動的に変更しますか

理由は、その自分自身を呼び出すonclickのリスナー内部に埋め込まれ、いただきました!これを回避する方法はありますか?

final RadioGroup rGroup3 = (RadioGroup) findViewById(R.id.radio_gcam); 

    OnClickListener radio_listener = new OnClickListener() { 

     public void onClick(View v) { 
      RadioButton button = (RadioButton) v; 
      no_of_gcams = 3; 
      gcam_names_array[0] = "Machine 1"; 
      gcam_names_array[1] = "Machine 2"; 
      gcam_names_array[2] = "Machine 3"; 
      gcam_names_array[3] = "Machine 4"; 

      for (int i = 0; i < no_of_gcams; i++) 
      { 

      RadioButton button2 = new RadioButton(ModbusDroid.this); 
      button2.setText(gcam_names_array[i]); 
      button2.setOnClickListener(radio_listener);  
      rGroup3.addView(button2); 

      } 
     } 
    }; 

    if (no_of_gcams == 0) 
    { 
    RadioButton button1 = new RadioButton(this); 
    button1.setText("Setup a G-CAM"); 

    button1.setOnClickListener(radio_listener); 
    rGroup3.addView(button1);} 

答えて

0

以下のスニペットを試してみて、それが役立ちます。

button2.setOnClickListener(this); 
+0

乾杯、シンプルだが効果的な:) – Monkins1010

0

こんにちは、今私は、問題のIVEが移動している "button2.setOnClickListener(これを);"他のどこかでこれを初期化することはできません。新しいチェックボックスを作成するチェックボックスを省いて、メニューにボタンを入れてモートオプションを作成してください。クリス

final OnClickListener radio_listener = new OnClickListener() { 

     public void onClick(View v) { 
      RadioButton button = (RadioButton) v; 
      //Toast.makeText(getBaseContext(),button.getText(), 5).show(); 

      no_of_gcams = no_of_gcams+1; 


      LayoutInflater factory2 = LayoutInflater.from(ModbusDroid.this); 
      textEntryNumericView = factory2.inflate(R.layout.write_value_numeric, null); 
      AlertDialog.Builder writeNumericDialogBuilder = new 
       AlertDialog.Builder(ModbusDroid.this); 
      writeNumericDialogBuilder.setTitle("Write to Register") 
       .setView(textEntryNumericView) 
       .setIcon(R.drawable.ic_dialog_edit) 
       .setMessage("Value to Write to Register") 
       .setNegativeButton("Cancel", 
         new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.dismiss(); 
        } 
       }) 
       .setPositiveButton("Write", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       gcam_names_array[0] = ((EditText) textEntryNumericView.findViewById(R.id.write_value_number)).getText().toString(); 
       Toast.makeText(getBaseContext(), "Writing Value: " + gcam_names_array[0], 5).show(); 

       RadioButton button2 = new RadioButton(ModbusDroid.this); 
       button2.setText(gcam_names_array[0]); 
       button2.setId(no_of_gcams); 
       button2.setOnClickListener(this);  
       rGroup3.addView(button2); 
       dialog.dismiss(); 
      } 

     }); 

      writeNumericRegisterDialog = writeNumericDialogBuilder.create(); 
      writeNumericRegisterDialog = writeNumericDialogBuilder.show(); 

     } 
     }; 
関連する問題