我的应用程序中有一个自定义 listView 我想实现我创建的全选按钮。
我的 ListView 看起来像。
[(图像)(文本)(复选框)]
我看过一些类似的问题,最常见的答案是该notifyDataSetChanged ()
方法的用户,我尝试过没有任何运气的研究和实施,我想知道你能想出一个解决方法或者给我一个例子来说明我是如何做到的可以实现方法
我的应用程序中有一个自定义 listView 我想实现我创建的全选按钮。
我的 ListView 看起来像。
[(图像)(文本)(复选框)]
我看过一些类似的问题,最常见的答案是该notifyDataSetChanged ()
方法的用户,我尝试过没有任何运气的研究和实施,我想知道你能想出一个解决方法或者给我一个例子来说明我是如何做到的可以实现方法
我无法使上述答案起作用,因此我使用了另一个答案的一部分并找到了答案。
我在全球范围内添加了这个。
ListView list;
//\\ 0 = None set \\// 1 = Select all \\// 2 = Un-Select all//\\
int selState = 0;
然后在我使用的 onClick 方法中
selState = 2;
list.invalidateViews();
selState 等于您要执行的功能
在适配器中,这是代码的最后一部分
switch(selState)
{
...
case 2:
CheckBox.setChecked(false);
break;
}
一种简单的方法是遍历 ListView 以获取每个项目 View,然后将其选中。我在下面提供了一些示例代码,因为您可以获取 ListView 作为 sampleListView 并具有 checkBoxId 复选框的 id:
// Loop through all the items in the list view
for(int viewIndex = 0; viewIndex < sampleListView.getCount(); viewIndex++)
{
// Get the current list item
View listItem = sampleListView.getChildAt(sampleListView.getFirstVisiblePosition() + viewIndex);
// Get the checkbox within the list item
CheckBox currentBox = (CheckBox)listItem.findViewById(R.id.checkBoxId);
// Check the checkbox
currentBox.setChecked(true);
}
您可以将此代码放在按钮的 OnClickListener() 中,它应该可以解决问题。