dynamic radio buttons are not working when click the radio group to checkchangelistner

﹥>﹥吖頭↗ 提交于 2019-12-12 02:58:23

问题


I created the radio buttons dynamically and set them in the radio group and it is working good but when i click the radio buttons to get the value but onclick is not working here is the code: in this when i click on checked change listner there is nothing happens i also taost it to show either onclick is working or not but nothing prints. please help thanks in advance.

   try {
        Thread.sleep(1000);

        voterNA=dbcon.GetVoterNA(id);

        LinearLayout ll=new LinearLayout(context);
        ll.setOrientation(LinearLayout.VERTICAL);
        ImageView iv=null;//new ImageView(context);
        LinearLayout.LayoutParams prams=new RadioGroup.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
        prams.setMargins(2, 2, 2, 2);

        rg=new RadioGroup(context);
        ll.setLayoutParams(prams);
        rg.setBackgroundColor(Color.LTGRAY);
        rg.setLayoutParams(prams);

        rg=dbcon.showNationalCan(rg, context,iv,voterNA);

        ll.addView(rg);

        ((ViewGroup)findViewById(R.id.NRG)).addView(ll);

        final RadioGroup rg1=(RadioGroup)findViewById(R.id.NRG);

        rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                for(int i=0; i<rg.getChildCount();i++)
                {
                    RadioButton rbtn=(RadioButton)rg.getChildAt(i);
                    if(rbtn.getId()==checkedId)
                    {
                        serial=rbtn.getId();
                        Toast.makeText(context, serial, 1).show();
                        flag=true;
                        return;
                    }
                    else
                    {
                        Toast.makeText(context, "Else", 1).show();

                        rbtn.setChecked(false);
                    }
                }
            }
        });

    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

来源:https://stackoverflow.com/questions/25617537/dynamic-radio-buttons-are-not-working-when-click-the-radio-group-to-checkchangel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!