从 Android 中的单选组中删除动态创建的单选按钮

发布于 2024-10-04 23:01:46 字数 694 浏览 11 评论 0原文

:) 我的 RadioGroup 的 RadioButton 是从 ArrayList(位于我的主要活动中)动态创建的,其中充满了如下链接:

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

然后我有一个名为“links”的新类,它是从我的主要活动中的菜单按钮调用的,它设置了一个很好的布局,每个链接有一个 radiobubtton(在滚动视图中)和一个“去!”页面底部的按钮(相对布局)。

除了一件事之外,这一切都很好。如果您使用后退按钮退出应用程序,然后通过单击 Android 菜单中的图标返回到应用程序,您将进入主 Activity,然后单击菜单中的“链接”按钮,它们每次都会翻倍。 ???像这样:

链接0
链接1
链接2
链接 0(再次)
链接 1(再次)
链接 2(再次)

每次您离开后返回到“链接”活动时,这会将链接附加到底部。有什么方法可以调用一个方法来清除链接活动中最后一个会话生成的所有单选按钮,然后再将它们放入 RadioGroup 中吗?我尝试将 onPause() 更改为 finish(); 我尝试使用 RadioGroup.destroyDrawingCache(); 释放缓存,但似乎没有任何效果。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

许久 2024-10-11 23:01:46

您可以删除使用以下方式动态创建的单选按钮:

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

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

RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroupnew1);
rg.clearCheck();
rg.removeAllViews();
静待花开 2024-10-11 23:01:46

我不会尝试这种“删除所有内容”的方法(这当然是可能的),而是尝试一些不同的方法 - 在首先填充单选按钮的代码中放置一个断点,然后按后退按钮,然后返回到应用程序,然后再次返回,再次回到应用程序,等等。

如果每次切换回应用程序时都命中断点,那么就有问题了。例如,通过将该代码从原来的位置移动到 OnCreate 来修复它,这样它只在 Activity 创建时调用一次。如果这不可行,则使用一些类变量,例如“boolean mRadioButtonsPopulated”,该变量在单选按钮填充代码之前进行检查,并在(第一个)单选按钮填充发生后设置为 true。

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.

野稚 2024-10-11 23:01:46

我明白了,我猜想即使在调用 onDestroy() 之后,内存中的位置仍然将 ArrayList 留在那里。我为解决这个问题所做的就是调用“.clear();”在我的 Android 应用程序的主 Activity 中实例化 ListArray 后。这样,每次打开应用程序时,它[数组列表]都会被实例化然后清除。效果就像一个魅力:)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文