Android 编程时如何正确使用哈希映射?
有人告诉我必须使用哈希映射来存储单选按钮。 (按钮作为键,它们的状态作为值)但我对 Java 和 Android 很陌生,所以这有点令人困惑。
我有一个包含多个问题和是或否答案的选择测试,我想计算是的答案。
也许这是一些简单的事情,但我无法弄清楚。
任何帮助将不胜感激。
谢谢
I was told that I have to use a hash-map to store radio buttons. (The buttons as the key and their state as value) but I am new to Java and Android so It's kinda confusing.
I have a choice test with multiple questions and yes or no answers and I want to count the yes answers.
Maybe it's some simple thing to do but I can't figure out.
Any help would be appreciated.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将对所有对象的引用添加到集合中,并对它们进行迭代,检查它们是否已被检查并对它们进行计数。
或者,您可以在所有按钮上注册一个 onClick 侦听器,并在单击按钮时增加/减少计数。
有多种方法可以实现这一目标。我不明白为什么你需要一个哈希图,因为选中状态在按钮对象本身上可用。你的哈希值没用。
You could add a reference to all of your objects to a collection and iterate over them checking whether they are checked and counting them up.
Alternatively you could register an onClick listener on all of the buttons and increment/decrement a count when they are clicked.
There are multiple ways you could achieve this. I dont see why you need a hash map though, as the checked state is available on the button object itself. Your hash is useless.