【发布时间】:2026-02-09 15:30:01
【问题描述】:
我有一个包含 649 个条目的 ListView。列表中的每个 View 都有两个 LinearLayout、一个 ImageView、几个 TextView 和一个 CheckBox。我目前有代码可以遍历所有 CheckBox 并对它们进行计数,还有更多代码可以确定哪些复选框被选中,以便我可以使用它们。但是,每当我选中其中一个 CheckBox 时,它上方和下方的每七个 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;
}
如果我可以添加任何额外信息,请询问!
【问题讨论】: