如何获取动态设置的radiobuttons的值?

时间:2021-06-25 21:19:13

I am having a dynamic set of radiobuttons whose value drawn from string array. I want to get the values of all selected radiobuttons on click of some button. How to do that ? Any idea

我有一组动态的radiobuttons,其值来自字符串数组。我想通过点击某个按钮获取所有选定的radiobutton的值。怎么做 ?任何的想法

Below is my working copy

以下是我的工作副本

public class MainActivity extends Activity {

    private ArrayList<Product>pProductArrayList;
    private ArrayList<SubCategory>pSubItemArrayList;
    private ArrayList<SubCategory>pSubItemArrayList2;
    private LinearLayout mLinearListView;
    boolean isFirstViewClick=false;
    boolean isSecondViewClick=false;
    RadioButton rdbtn;
    private RadioGroup radioGroup;
    private String[] shownames = {"AA", "BB", "CC"};                                              //Dynamic Show Names
    private String[] application = {"DD", "EE", "FF", "GHI"};                    //Dynamic Applicaiton Names
    private String[] device = {"JKL", "MNO", "PQR", "STU"};                                                //Dynamic Device Names

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mLinearListView = (LinearLayout) findViewById(R.id.linear_listview);

        ArrayList<ItemList> mItemListArray=new ArrayList<ItemList>();
        mItemListArray.add(new ItemList("", ""));  

        ArrayList<ItemList> mItemListArray2=new ArrayList<ItemList>();
        mItemListArray2.add(new ItemList("", ""));  

        pSubItemArrayList=new ArrayList<SubCategory>();
        pSubItemArrayList2=new ArrayList<SubCategory>();

        pSubItemArrayList.add(new SubCategory("Select Application/Device", mItemListArray));                                       //Static Header
        pSubItemArrayList2.add(new SubCategory("Select Device", mItemListArray2));                                                 //Static Header

        pProductArrayList=new ArrayList<Product>();

        for (int i = 0; i < shownames.length; i++){
            pProductArrayList.add(new Product(shownames[i], pSubItemArrayList));
        }

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

            LayoutInflater inflater = null;
            inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View mLinearView = inflater.inflate(R.layout.row_first, null);

            final TextView mProductName = (TextView) mLinearView.findViewById(R.id.textViewName);
            final RelativeLayout mLinearFirstArrow=(RelativeLayout)mLinearView.findViewById(R.id.linearFirst);
            final ImageView mImageArrowFirst=(ImageView)mLinearView.findViewById(R.id.imageFirstArrow);
            final LinearLayout mLinearScrollSecond=(LinearLayout)mLinearView.findViewById(R.id.linear_scroll);

            if(isFirstViewClick==false){
            mLinearScrollSecond.setVisibility(View.GONE);
            mImageArrowFirst.setBackgroundResource(R.drawable.arw_lt);
            }

            else{
                mLinearScrollSecond.setVisibility(View.VISIBLE);
                mImageArrowFirst.setBackgroundResource(R.drawable.arw_down);
            }

            mLinearFirstArrow.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {

                    if(isFirstViewClick==false){
                        isFirstViewClick=true;
                        mImageArrowFirst.setBackgroundResource(R.drawable.arw_down);
                        mLinearScrollSecond.setVisibility(View.VISIBLE);

                    }else{
                        isFirstViewClick=false;
                        mImageArrowFirst.setBackgroundResource(R.drawable.arw_lt);
                        mLinearScrollSecond.setVisibility(View.GONE);   
                    }
                    return false;
                } 
            });


            final String name = pProductArrayList.get(i).getpName();
            mProductName.setText(name);

            /**
             * 
             */
            for (int j = 0; j < pProductArrayList.get(i).getmSubCategoryList().size(); j++) {

                LayoutInflater inflater2 = null;
                inflater2 = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View mLinearView2 = inflater2.inflate(R.layout.row_second, null);

                TextView mSubItemName = (TextView) mLinearView2.findViewById(R.id.textViewTitle);
                final RelativeLayout mLinearSecondArrow=(RelativeLayout)mLinearView2.findViewById(R.id.linearSecond);
                final ImageView mImageArrowSecond=(ImageView)mLinearView2.findViewById(R.id.imageSecondArrow);
                final LinearLayout mLinearScrollThird=(LinearLayout)mLinearView2.findViewById(R.id.linear_scroll_third);

                if(isSecondViewClick==false){
                    mLinearScrollThird.setVisibility(View.GONE);
                    mImageArrowSecond.setBackgroundResource(R.drawable.arw_lt);
                    }
                    else{
                        mLinearScrollThird.setVisibility(View.VISIBLE);
                        mImageArrowSecond.setBackgroundResource(R.drawable.arw_down);
                    }

                mLinearSecondArrow.setOnTouchListener(new OnTouchListener() {

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {

                            if(isSecondViewClick==false){
                                isSecondViewClick=true;
                                mImageArrowSecond.setBackgroundResource(R.drawable.arw_down);
                                mLinearScrollThird.setVisibility(View.VISIBLE);

                            }else{
                                isSecondViewClick=false;
                                mImageArrowSecond.setBackgroundResource(R.drawable.arw_lt);
                                mLinearScrollThird.setVisibility(View.GONE);    
                            }
                            return false;
                        } 
                    });


                final String catName = pProductArrayList.get(i).getmSubCategoryList().get(j).getpSubCatName();
                mSubItemName.setText(catName);


                for (int k = 0; k < pProductArrayList.get(i).getmSubCategoryList().get(j).getmItemListArray().size(); k++) {

                        LayoutInflater inflater3 = null;
                        inflater3 = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        View mLinearView3 = inflater3.inflate(R.layout.row_third, null);

                        radioGroup = (RadioGroup) mLinearView3.findViewById(R.id.myRadioGroup);

                        for(int m=0;m<application.length;m++) {
                               rdbtn = new RadioButton(this);
                               rdbtn.setId(m);
                               rdbtn.setText(application[m]);
                               radioGroup.addView(rdbtn);
                        }
                        mLinearScrollThird.addView(mLinearView3);

                }


                for (int l = 0; l < pProductArrayList.get(i).getmSubCategoryList().get(j).getmItemListArray().size(); l++) {

                    LayoutInflater inflater4 = null;
                    inflater4 = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    View mLinearView4 = inflater4.inflate(R.layout.row_third, null);

                    radioGroup = (RadioGroup) mLinearView4.findViewById(R.id.myRadioGroup);

                    for(int m=0;m<device.length;m++) {
                           rdbtn = new RadioButton(this);
                           rdbtn.setId(m);
                           rdbtn.setText(device[m]);
                           radioGroup.addView(rdbtn);
                    }
                        mLinearScrollThird.addView(mLinearView4);

                }

                mLinearScrollSecond.addView(mLinearView2);

            }

            mLinearListView.addView(mLinearView);
        }       


    }

}

1 个解决方案

#1


If you want to get the values of the checked radioButton (inside the radioGroup it's only one item) when you click a button you could use this:

如果你想获得被检查的radioButton的值(在radioGroup中它只是一个项目),当你点击一个按钮时你可以使用它:

RadioGroup group = (RadioGroup)findViewById(R.id.radio_group_id);
Button button = (Button)findViewById(R.id.button_id);

//if you want to print radio's value in a textView
TextView label  = (TextView)findViewById(R.id.text_view_id);

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            int checkedId = group.getCheckedRadioButtonId();

            RadioButton radio = (RadioButton)findViewById(checkedId);
            label.setText(radio.getText());

        }
    });

#1


If you want to get the values of the checked radioButton (inside the radioGroup it's only one item) when you click a button you could use this:

如果你想获得被检查的radioButton的值(在radioGroup中它只是一个项目),当你点击一个按钮时你可以使用它:

RadioGroup group = (RadioGroup)findViewById(R.id.radio_group_id);
Button button = (Button)findViewById(R.id.button_id);

//if you want to print radio's value in a textView
TextView label  = (TextView)findViewById(R.id.text_view_id);

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            int checkedId = group.getCheckedRadioButtonId();

            RadioButton radio = (RadioButton)findViewById(checkedId);
            label.setText(radio.getText());

        }
    });