How do I identify which radio buttons were selected from each group?

0

I'm making an application where groups of Radio Buttons are generated whose names and values depends on a json. But, I have a detail, if for example 3 groups of radio buttons are generated with 3 options each, what I want is to know that radio buttons was selected from each group. Any suggestions on how I can do it These can be 2 groups, 10 groups, etc.

LayoutInflater inflater = LayoutInflater.from(Vista_producto.this);
                        int id = R.layout.botones;

                        LinearLayout linearLayout = (LinearLayout) inflater.inflate(id, null, false);
                        TextView textView = (TextView) linearLayout.findViewById(R.id.textView3);
                        textView.setText(std_name);
                        RadioGroupt = (RadioGroup) linearLayout.findViewById(R.id.RadioGroup1);
                        //LinearLayout linearLayout1 = (LinearLayout) linearLayout.findViewById(R.id.layout1);
                        //RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
                        float density = getResources().getDisplayMetrics().density;
                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                                (int)(55*density),
                                (int)(45*density));
                        int margin = (int)(6*density);
                        params.setMargins(margin, margin, margin, margin);

                        i= i + 1;
                        layout.addView(linearLayout);
                        String std_item = jsonObject1.getString("std_item");

                        JSONObject js6 = new JSONObject(std_item);
                        int j = 0;
                        Iterator<String> keys1 = js6.keys();
                        while (keys1.hasNext()) {
                            String key1 = keys1.next();
                            Log.i("Parser", "objeto : " + key1);
                            JSONObject ke = js6.getJSONObject(key1);
                            String std_item_name = ke.getString("std_item_name");
                            Rboton = new RadioButton(getApplicationContext());
                            Rboton.setText(std_item_name);
                            Rboton.setId(j);
                            RadioGroupt.addView(Rboton);
                            //Rboton.setTextColor(Color.BLACK);
                            Rboton.setButtonDrawable(R.drawable.borde);
                            //Rboton.setButtonDrawable(R.drawable.boton_p);

              Rboton.setBackgroundResource(R.drawable.rb_background_lluvia);
                            Rboton.setGravity(Gravity.CENTER);
                            Rboton.setTextColor(Color.BLACK);
                            Rboton.setLayoutParams(params);

    
asked by Maria Isabel 12.10.2018 в 17:19
source

1 answer

0

you could tag your buttons in the following way

Example of a button

<Button
   android:id="@+id/buttonTag"
   android:layout_width="match_parent"
   android:layout_height="50dp"
   android:text="Hola"/>

In your Java class you add a tag in the following way

Button buttonTag = (Button) findViewById(R.id.buttonTag);
// set tag
button.setTag("boton_hola);

And you get the tag in the following way

String tag = (String) buttonTag.getTag(); 

For what I see you should only add the tag, and then with the last line get the tag that would be the id that you should assign to differentiate the buttons and in this way to know which is which. Greetings.

    
answered by 13.10.2018 в 05:25