如何激活列表活动中的复选标记?
我有一个 ListActivity ,其数组适配器声明为 arrayAdapter = new ArrayAdapter
这显示了一堆在最右侧带有复选标记的行。您能告诉我如何获取这些复选标记的参考或如何选中/取消选中它们吗?
I have a ListActivity with an array adapter declared like arrayAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_checked);
This shows a bunch of rows with checkmarks on the far right. Can you tell me how to get a reference to those checkmarks or how to check/uncheck them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
CheckedTextView 本身处理复选框。它作为 onListItemClick 处理程序中的第二个参数 (View v) 传入。因此,您可以按如下方式简化代码:
The CheckedTextView itself handles the checkbox. It is passed in as the second argument (View v) in the onListItemClick handler. So, you can simplify your code as follows:
我遇到了类似的问题,并尝试了这里提供的解决方案,但仍然遇到很多问题。
我只想有一个包含可选“测试用例”的列表和两个按钮“选择所有测试”和“运行选定的测试”(因此我不想只有一个 ListActivity)。
正如“JDC”所提到的,getChildAt(和 getChildCount)指的是当前显示的项目,但我的列表不适合屏幕,因此我无法使用它来选择所有列表项。
另外,如果我使用 CheckedTextView 的 setChecked ,我会遇到滚动列表后选择消失的问题。
我的解决方案是下面的代码,通过使用 ListView 的 getCount 和 setItemChecked 来修复这些问题(另请参阅源代码中的注释)。此外,它还显示了如何检索已检查的项目。
这也是适当的布局(main.xml):
I had a similar problem and tried the solutions provided here but I still had a lot of problems.
I just wanted to have a list with selectable "test cases" and two buttons "select all tests" and "run selected tests" (therefore I didn't want to have just a ListActivity).
As mentioned by "JDC" getChildAt (and getChildCount) refer to the currently displayed items but my list didn't fit on the screen and therefore I couldn't use it to select all list items.
Additionally if I used setChecked of the CheckedTextView I had the problem that the selection disappeared as soon as I scrolled the list.
My solution is the code below that fixes these issues by using getCount and setItemChecked of the ListView (see also the comments in the source code). Additionally it shows how to retrieve the checked items.
Here is also the appropriate layout (main.xml):
我能做的最接近的事情是在单击单元格后更改复选标记:
我仍然希望能够在用户不触摸任何单元格的情况下设置复选标记。
The closest I can do is change the checkmark after a cell is clicked using:
I would still like to be able to set the checkmarks without the user touching any cells.