在 ListView 中使用 CheckBoxes 检查每七个框
我有一个包含 649 个条目的 ListView。列表中的每个View都有两个LinearLayout、一个ImageView、几个TextView和一个CheckBox。我目前有代码可以遍历所有复选框并对其进行计数,还有更多代码可以确定哪些复选框已被选中,以便我可以使用它们。然而,每当我选中其中一个复选框时,其上方和下方的每七个复选框也会神奇地被选中。对选中框进行计数的方法返回我实际选中的数量,但获取所有选中框的索引的方法只返回第一个选中的 x,无论我是否选中了它们或距它们 7 的那些。例如,如果我检查数字 21,该方法将返回索引 3。我已经包含了一些相关的代码段: 列表中每个View的布局代码:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/id"
android:layout_width="50dp"
android:layout_height="50dp"
android:textSize="20sp"/>
<LinearLayout
android:layout_width="180dp"
android:layout_height="50dp"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/name"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#777777"
android:id="@+id/type"/>
</LinearLayout>
<CheckBox
android:layout_gravity="right|center_horizontal"
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"/>
检查计数方法:
public int[] whichAreChecked() //TODO: this should work.
{
int listItemCount = ChooserList.getChildCount();
ArrayList<Integer> ints=new ArrayList<Integer>();
for(int i=0; i<listItemCount; i++)
{
CheckBox cbox = (CheckBox) ((View)ChooserList.getChildAt(i)).findViewById(R.id.check);
if(cbox.isChecked())
ints.add(i);
}
Log.e("durr", ""+ints.size());
int[] result=new int[ints.size()];
for(int i=0; i<result.length; i++)
result[i]=ints.get(i);
return result;
}
检查计数方法:
public int countChecks()
{
int checked=0;
int listItemCount = ChooserList.getChildCount();
for(int i=0; i<listItemCount; i++)
{
CheckBox cbox = (CheckBox) ((View)ChooserList.getChildAt(i)).findViewById(R.id.check);
if(cbox.isChecked())
checked++;
}
Log.e("countChecks()", ""+checked);
return checked;
}
如果我可以添加任何额外信息,请询问!
I have a ListView that has 649 entries. Each View in the list has two LinearLayouts, an ImageView, a few TextViews, and a CheckBox. I currently have code to go through all the CheckBoxes and count them, and more code to figure out which ones are checked so I can use them. However, whenever I check one of the CheckBoxes, every seventh CheckBox above and below it magically become checked as well. The method to count the checked boxes returns the number I actually checked, but the method to get the indices of all the checked boxes just returns the first x checked, regardless of whether I checked them or the ones 7 away from them. For example, if I check number 21, the method returns index 3. I have included some of the relevant code segments:
The layout code for each View in the list:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/id"
android:layout_width="50dp"
android:layout_height="50dp"
android:textSize="20sp"/>
<LinearLayout
android:layout_width="180dp"
android:layout_height="50dp"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/name"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#777777"
android:id="@+id/type"/>
</LinearLayout>
<CheckBox
android:layout_gravity="right|center_horizontal"
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"/>
Check counting method:
public int[] whichAreChecked() //TODO: this should work.
{
int listItemCount = ChooserList.getChildCount();
ArrayList<Integer> ints=new ArrayList<Integer>();
for(int i=0; i<listItemCount; i++)
{
CheckBox cbox = (CheckBox) ((View)ChooserList.getChildAt(i)).findViewById(R.id.check);
if(cbox.isChecked())
ints.add(i);
}
Log.e("durr", ""+ints.size());
int[] result=new int[ints.size()];
for(int i=0; i<result.length; i++)
result[i]=ints.get(i);
return result;
}
Check counting method:
public int countChecks()
{
int checked=0;
int listItemCount = ChooserList.getChildCount();
for(int i=0; i<listItemCount; i++)
{
CheckBox cbox = (CheckBox) ((View)ChooserList.getChildAt(i)).findViewById(R.id.check);
if(cbox.isChecked())
checked++;
}
Log.e("countChecks()", ""+checked);
return checked;
}
If I can add any extra info, please ask!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,我认为您误解了
getChildCount()
和getChildAt()
方法的作用。这些实际上只会获得渲染视图。除非我在这里误读了一些内容,否则如果您有 650 行左右,您将无法使用正在渲染的视图来完成您想要的任务。相反,您应该实现一个onItemClickListener
(或者,单击复选框时触发的东西),并在选中该复选框后,将该状态保存到您的模型中。Hmmmm I think you misunderstood what the
getChildCount()
andgetChildAt()
methods do. Those will actually get only the rendered views. Unless I misread something here, if you have 650ish rows you cannot accomplish what you want with the views that are being rendered. Instead, you should implement anonItemClickListener
(or, alternatively, something that fires when the checkboxes are clicked) and after the checkbox is checked, save that state into your model.我也面临着类似的问题,所以经过大量阅读后,我解决了这个问题,如下所示:
这里我在 getView() 上所做的事情我取消选中所有复选框并再次手动检查那些我需要根据检查进行检查的复选框到它对应的文本视图。因此,如果用户在选中第一个复选框后向下滚动,则视图中的所有复选框都将被取消选中,如果他再次向上滚动,那么所有复选框也将被取消选中,但他之前单击的复选框将被再次重新选中。
I was also facing a similar king of problem, so after lot of reading I solved this problem like this:
Here what I am doing that on getView() I am unchecking all the checkboxes and checking again manually those which I need to be checked according to the textview it corresponds. So if the user scroll down after checking the first checkbox, all the checkbox in the view will get unchecked and if he again scrolls up then also all the checkboxes will be unchecked but then the one he clicked before will be again rechecked.