remove dynamicallly created radiobuttons from a radiogroup in Android
Asked Answered
A

3

6

:) My RadioGroup's RadioButtons get dynamically created from an ArrayList (which resides in my main activity) full of links like so:

ArrayList = { "hxxp://helloworld.com", "hxxp://helloworld2.net", ..., "hxxp://whatever.com" }

then I have a new class called "links" that gets called from a menu button in my main activity which sets a nice layout with a radiobubtton per link (in a scrollview) and a "go!" button on the bottom of the page (relative layout).

This all works great except for one thing. If you exit the application using the back button, and go back into the application by clicking on the icon in the Android menu, you get to the main activity, then click the "links" button in the menu and they are doubled each time. ??? like so:

link 0
link 1
link 2
link 0 (again)
link 1 (again)
link 2 (again)

This appends the links to the bottom each time you come back to the "links" activity from leaving. Is there some way I can call a method to clear all radio buttons generated form the last session in the links activity before putting them into the RadioGroup? I tried changing my onPause() to finish(); I tried freeing the cache with RadioGroup.destroyDrawingCache(); nothing seems to do it.

Amir answered 27/11, 2010 at 21:22 Comment(0)
M
0

Instead of this 'delete everything' approach (which is surely possible) I would try something different - place a breakpoint in the code that firstly populates your radio buttons and press the back button, then go back into the app, then again back, again back into the app, etc.

If the breakpoint gets hit each time you switch back into the application, then there is your problem. Fix it by moving that code from where it is to OnCreate for example, so it is only called once at Activity creation time. If that is not doable, then use some class variable like 'boolean mRadioButtonsPopulated' which gets checked just before the radio buttons population code, and gets set to true after the (first) radio button population happened.

Mccutcheon answered 27/11, 2010 at 21:47 Comment(1)
You're right, I just wish there were better methods for RadioGroup like "refresh();" or "clearButtons();" which would be awesome for those who automatically generate the buttons from, say, an XML file in the project apk.Amir
C
13

You can remove your radio buttons which were dynamically created using like this:

RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroupnew1);
rg.clearCheck();
rg.removeAllViews();
Chemulpo answered 24/7, 2013 at 14:56 Comment(0)
M
0

Instead of this 'delete everything' approach (which is surely possible) I would try something different - place a breakpoint in the code that firstly populates your radio buttons and press the back button, then go back into the app, then again back, again back into the app, etc.

If the breakpoint gets hit each time you switch back into the application, then there is your problem. Fix it by moving that code from where it is to OnCreate for example, so it is only called once at Activity creation time. If that is not doable, then use some class variable like 'boolean mRadioButtonsPopulated' which gets checked just before the radio buttons population code, and gets set to true after the (first) radio button population happened.

Mccutcheon answered 27/11, 2010 at 21:47 Comment(1)
You're right, I just wish there were better methods for RadioGroup like "refresh();" or "clearButtons();" which would be awesome for those who automatically generate the buttons from, say, an XML file in the project apk.Amir
A
0

I got it, I guess the place in memory left the ArrayList there even after onDestroy() was called. All I did to fix this was call ".clear();" on the ListArray right after instantiating it in the main Activity of my Android application. This way each time the application is opened it [the arraylist] gets instantiated then cleared. Works like a charm :)

Amir answered 27/11, 2010 at 21:51 Comment(2)
Heh, it sure does - but think about the extra processing needed to rebuild each time the Radio button group. Otherwise, it is your design choiceMccutcheon
Is that a sentence? "think about the extra processing needed to rebuild each time the Radio button group" ? plus the radiobutton group is a direct result of the user of the application. That means things are in or out due to input. Of course I need to "rebuild each time the radio button group." Thanks.Amir

© 2022 - 2024 — McMap. All rights reserved.