开发者

remove dynamicallly created radiobuttons from a radiogroup in Android

:) 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" tha开发者_开发问答t 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.


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

RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroupnew1);
rg.clearCheck();
rg.removeAllViews();


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.


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 :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜