1

Привет всем, Мы хотим добавить динамическое динамическое отображение рядом с радиогруппой. Основываясь на ответе службы, нам нужно добавить ярлык к радиогруппе без использования xml. В некоторых ситуациях на основе максимальной длиной радиоКак добавить текстовое представление в радиогруппу динамически в android

[Here we are showing checkboxes,but based on response it will changes to radiogroup with in radiobutton]

здесь ниже кода

final RadioGroup rg = new RadioGroup(this); //create the RadioGroup 
      rg.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
      rg.setOrientation(RadioGroup.VERTICAL);//or RadioGroup.VERTICAL 
      rg.setGravity(Gravity.CENTER); 
      final RadioButton[] rb = new RadioButton[items_list.size()]; 
      final CheckBox[] cb = new CheckBox[items_list.size()]; 

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

       LinearLayout ll_orientaion = new LinearLayout(this); 
       ll_orientaion.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 2.0f)); 
       ll_orientaion.setOrientation(LinearLayout.HORIZONTAL); 
       ll_orientaion.setBackgroundColor(getResources().getColor(R.color.white)); 
       ll_orientaion.setWeightSum(2); 
       ll_orientaion.setGravity(Gravity.CENTER); 


       if (max == 1) { 

        rb[i] = new RadioButton(this); 
        rb[i].setText(pd.getName()); 
        rb[i].setGravity(Gravity.CENTER); 
        ll_orientaion.addView(rb[i]); 

        if (pd.getSelected_status().equals("true")) { 
         rb[i].setChecked(true); 

        } 

        hashMap_rdlist.put(rb[i].getText().toString(), rdd); 

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
         @Override 
         public void onCheckedChanged(RadioGroup group, int checkedId) { 

          ProductOptions rd_getlist = new ProductOptions(); 

          int checkedRadioButtonId = rg.getCheckedRadioButtonId(); 
          RadioButton radioButton = (RadioButton) findViewById(checkedRadioButtonId); 
          // Toast.makeText(ProductOptionsActivity.this, radioButton.getText(), Toast.LENGTH_SHORT).show(); 

          String rd_name = (String) radioButton.getText(); 

        }); 
       } else { 

        cb[i] = new CheckBox(this); 
        cb[i].setButtonTintList(ColorStateList.valueOf(getColor(R.color.colorPrimary))); 
        cb[i].setText(pd.getName()); 
        cb[i].setGravity(Gravity.CENTER); 
        ll_orientaion.addView(cb[i]); 


        } 

        final int finalI = i; 

        cb[i].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
         @Override 
         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 

          ProductOptions rd_getlist = new ProductOptions(); 
          String check_name = (String) cb[finalI].getText(); 

          if (isChecked) { 

           check_count++; 


          } else { 
           check_count--; 

          } 
         } 
        }); 
       } 
       // Log.d("checklist",""+productoptions_skuId); 

       TextView tv = new TextView(this); 
       LinearLayout.LayoutParams tv1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f); 
       tv.setLayoutParams(tv1); 
       tv1.weight = 2; 
       tv1.gravity = Gravity.CENTER; 
       tv.setText(pd.getSalePrice()); 
       tv.setTextSize(14); 
       tv.setGravity(Gravity.END); 
       if (pd.getSalePrice().equals("0.0")) { 
        tv.setVisibility(View.INVISIBLE); 
       } 
       ll_orientaion.addView(tv); 
       rg.addView(ll_orientaion); 
      } 

Основная проблема, когда мы добавляем LinearLayout к radiGroup View становятся совершенное, но RadioButton действие не работает т.е.

rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {}

Пожалуйста Gui de us Успешное спасибо!

+0

'RadioGroup' будет работать только для своих непосредственных детей, которые либо сами, либо являются подклассом« RadioButton ». Кажется, вы добавляете «LinearLayout» в качестве детей «RadioGroup». – Shaishav

ответ

0

Создайте пользовательский адаптер для списка с CheckedTextView.

+0

ОК. Но когда появятся переключатели, это создаст проблему –

+0

Какая проблема у вас возникает? –

+0

проблема означает, что checkbutton имеет несколько опций , но у radobutton есть singleoption checked –

Смежные вопросы