gpt4 book ai didi

android - 在 ListView 中用 CheckBoxes 检查每七个框

转载 作者:行者123 更新时间:2023-11-29 22:26:07 24 4
gpt4 key购买 nike

我有一个包含 649 个条目的 ListView。列表中的每个 View 都有两个 LinearLayout、一个 ImageView、几个 TextView 和一个 CheckBox。我目前有代码来遍历所有 CheckBoxes 并计算它们,还有更多代码来确定哪些被选中,以便我可以使用它们。但是,每当我选中其中一个 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;
}

如果我可以添加任何额外的信息,请询问!

最佳答案

嗯,我认为您误解了 getChildCount()getChildAt() 方法的作用。这些实际上只会获得呈现 的 View 。除非我在这里误读了什么,否则如果你有 650 多行,你将无法通过正在呈现的 View 完成你想要的。相反,您应该实现一个 onItemClickListener(或者,当复选框被单击时触发的东西)并在复选框被选中后,将该状态保存到您的模型中。

关于android - 在 ListView 中用 CheckBoxes 检查每七个框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5925119/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com