Android: How to get the text of a dynamically created radio button selected by the user?

How can I get the dynamically created radio button text selected by the user? Here is my code:

RadioGroup radiogroup = (RadioGroup) findViewById(R.id.rdbGp1); // layout params to use when adding each radio button LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams( RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT); for (int i = 0; i < 4; i++){ final RadioButton newRadioButton = new RadioButton(this); c3 = db.getAns(3); for (int j=0;j<i;j++) c3.moveToNext(); label = c3.getString(0); newRadioButton.setText(label); newRadioButton.setId(6); radiogroup.addView(newRadioButton, layoutParams); 

Waiting for an answer, Maksoud

+4
source share
5 answers

Surprised there is no easier way. If you are going to do something special, although based on which button you should probably check the identifier instead of the label.

 radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { void onCheckedChanged(RadioGroup rg, int checkedId) { for(int i=0; i<rg.getChildCount(); i++) { RadioButton btn = (RadioButton) rg.getChildAt(i); if(btn.getId() == checkedId) { String text = btn.getText(); // do something with text return; } } } }); 
+16
source

I think there is an easier way to do this ...

I just created a radio button with a verified button id and it works fine.

The solution looks like this:

 RadioButton TheTextIsHere = (RadioButton) findViewById(RadioGroup.getCheckedRadioButtonId()); 

So now you have a RadioButton that links to a RadioButton that is registered with a RadioGroup, and then you can easily ...

 TheTextIsHere.getText().toString(); 

I hope I helped :)

+8
source

Old question, but this answer may help someone else.

I solved the problem in order to get the text from RadioButton , as shown below, without any for loop. It works for me, but I used xml, but I think that the principle will work anyway.

The // code is only needed if no RadioButton is a preliminary check, because radioBtnChecked will be -1 if RadioButton not selected. Therefore, the application crashes because findviewbyid(-1) invalid. At least in xml you pre-check RadioButton on android:checked="true" .

 RadioGroup radioGroup1 = (RadioGroup) findViewById(R.id.radiogroup1); int radioBtnChecked = radioGroup1.getCheckedRadioButtonId(); // if (radioBtnChecked <= 0) { // radioText = "None selected"; // } // else { RadioButton rBtn = (RadioButton) findViewById(radioBtnChecked); radioText = rBtn.getText().toString(); 
+3
source

In this situation, it is better to use HasMap. HashMap are designed to quickly retrieve values ​​... Of course, in your particular case, only 4 switches are used, so you won’t notice the difference. However, I always prefer this solution.

Create a member variable for HasMap:

 Map<Integer, RadioButton> mapping = new HashMap<Integer, RadioButton>(); 

In the for loop, where you create your RadioButtons, add them to the hasmap:

 { ... // your for-loop int id = <your id here> newRadioButton.setId(id); // set the id mapping.put(id, newRadioButton); // store the id as the key-value ... // continue with your for-loop } 

Finally, in your onCheckedChangeListener you can extract the RadioButton from the HashMap. Note. HashMap does not loop through all of its entries to extract the value, so it will be (slightly) faster. Of course, in this case you have to pay with memory:

 radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup rg, int checkedId) { String txt = ((RadioButton)mapping.get(checkedId)).getText(); // do something with your text } }); 
0
source

There is a hacker way to do this. For each switch, you need to have a positive integer as an identifier. Then, using this identifier, you can refer to the selected radio button. Here is the code:

 private void addButtons(String[] taskNames) { //You can define your radio group this way //or you can define it in onCreate. NOTE: if you //define it in onCreate, make sure you do a //rGroup.removeAllViews() or rGroup.removeView(child) rGroup = new RadioGroup(this); //hash code is the ID we will give to the radio buttons int hash; //going through the list of names and making radio buttons //out of them and putting them into a radio group for(String name : taskNames) { //making a button RadioButton button = new RadioButton(this); //setting the button text button.setText(name); //setting the button ID by finding it hashCode //Note that the ID MUST be a positive number hash = Math.abs((name).hashCode()); button.setId(hash); //adding to the radio button group rGroup.addView(button); } //Then you can add the radio group to your desired layout from the xml file LinearLayout desiredLayout = (LinearLayout) findViewById(R.id.desireLinearLayout); desiredLayout.addView(rGroup); } //here is a how to get the checked radio button private void onClickSubmit() { //for instance you can add the name to a DB DatabaseHandler db = new DatabaseHandler(this); try { //get the ID of the button (ie the hashCode we assigned to it int id = rGroup.getCheckedRadioButtonId(); //Getting the radio button RadioButton rbChecked = (RadioButton) rGroup.findViewById(id); //getting the name of the radio button String rbName = rbChecked.getText().toString(); //adding the name to the DB db.addName(rbName); //showing a friendly message to the user that the operation has been successful Toast.makeText(this, "Yay, name added", Toast.LENGTH_SHORT).show(); } catch (Exception e) { Toast.makeText(this, "Can't submit", Toast.LENGTH_SHORT).show(); } } 

Hashcodes are deterministic, so it’s safe to use them, but since we do Math.abs, then we create space for the hash functions of 2 items with the same value, because we eliminate the negative part. But for now, this works great for me. But you can do all kinds of creative things to avoid collisions. I'm sure you find out :)

0
source

Source: https://habr.com/ru/post/1313194/


All Articles