动态添加无线电单一选择

时间:2017-01-19 06:27:00

标签: android json radio-button

我无法单独选择radio button动态添加到radio group。请帮忙选择它的单一选择。以下是我的代码。

我的片段类。

 public class fragment4 extends Fragment {
        View v;
        LinearLayout my_layout;
        //CheckBox checkBox;
        RadioButton checkBox;
        JSONArray jsonArray;
        Context ctx;
    RadioGroup rg;    /* MediaPlayer mp;*/

        public fragment4(){
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // 1.

        }


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            v = inflater.inflate(R.layout.farg4, container, false);
          my_layout = (LinearLayout)v.findViewById(R.id.my_layout);

            getcategory();
            return my_layout;

        }

        public void getcategory() {
            StringRequest request = new StringRequest(Request.Method.POST,
                    StaticDataUtility.Server_URL + "" + StaticDataUtility.getsubcategory, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject joResp = new JSONObject(response);
                        ArrayList<productinfo> sliderList = new ArrayList<>();
                        int success = joResp.getInt("Success");
                        if (success == 1) {
    //                        industryListBeanArrayList.clear();
                            jsonArray = joResp.getJSONArray("result");
    //                        JSONObject joResult = joResp.getJSONObject("result");
    //                        JSONArray jaDetail = joResult.getJSONArray("industry_detail");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject joDetail = jsonArray.getJSONObject(i);
    //                            productinfo slider = new productinfo();
                                if (joDetail.has("subcat_name")) {
                                    rg= new RadioGroup(getContext());
                                    rg.setOrientation(RadioGroup.VERTICAL);
    //                                TableRow row =new TableRow(getActivity());
    //                                row.setId(i);
    //                                row.setLayoutParams(new TableRow.LayoutParams(Toolbar.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
                                    checkBox = new RadioButton(getActivity());
                                   /* checkBox.setOnCheckedChangeListener(v.getContext());*/
                                    checkBox.setId(i);
                                    checkBox.setText(joDetail.getString("subcat_name"));
                                    //row.addView(checkBox);
                                    rg.addView(checkBox);
                                 // my_layout.addView(rg);

                                   my_layout.addView(rg);

                                    // ((ViewGroup)v.findViewById(R.id.radgrpcat)).addView(my_layout);
                                    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                                        @Override
                                        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                                            for (int p=0;p<=jsonArray.length();p++){
                                                if(b){
                                                    String valchk=checkBox.getText().toString().trim();
                                                    FilteredBy.valchk2= new String[]{valchk};
                                                }
                                            }
                                            Log.e("checkbox", String.valueOf(FilteredBy.valchk2.length));
                                        }
                                    });
                                }



                            }

                        } else {
                            Toast.makeText(getActivity(), "Sorry Data Are Not Available ", Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },


                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {
                        }
                    }) {
                @Override
                protected Map<String, String> getParams() {
                    HashMap<String, String> params = new HashMap<>();
                    params.put("cat_id", "363");
                    Log.e("params", params.toString());
                    return params;
                }
            };

            request.setRetryPolicy(new
                    DefaultRetryPolicy(60000,
                    DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
            // Adding request to request queue
            Volley.newRequestQueue(getActivity()).add(request);

        }

    }

here showing all button is selected in image

1 个答案:

答案 0 :(得分:1)

正如迈克在评论中所说,你每次都在创建RadioGroup。你需要在for循环之前放置以下代码

rg= new RadioGroup(getContext());
rg.setOrientation(RadioGroup.VERTICAL);
my_layout.addView(rg);

代码应如下所示(如果我正确理解您的代码):

rg= new RadioGroup(getContext());
rg.setOrientation(RadioGroup.VERTICAL);
my_layout.addView(rg);
for (int i = 0; i < jsonArray.length(); i++) {