在 ListView 中使用 CheckBoxes 检查每七个框

发布于 2024-11-05 20:57:16 字数 2313 浏览 0 评论 0原文

我有一个包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伪装你 2024-11-12 20:57:16

嗯,我认为您误解了 getChildCount()getChildAt() 方法的作用。这些实际上只会获得渲染视图。除非我在这里误读了一些内容,否则如果您有 650 行左右,您将无法使用正在渲染的视图来完成您想要的任务。相反,您应该实现一个 onItemClickListener (或者,单击复选框时触发的东西),并在选中该复选框后,将该状态保存到您的模型中。

Hmmmm I think you misunderstood what the getChildCount() and getChildAt() 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 an onItemClickListener (or, alternatively, something that fires when the checkboxes are clicked) and after the checkbox is checked, save that state into your model.

一笑百媚生 2024-11-12 20:57:16

我也面临着类似的问题,所以经过大量阅读后,我解决了这个问题,如下所示:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.listview, null);
            holder = new ViewHolder();
            holder.nameView = (TextView)convertView.findViewById(R.id.textView1);
            holder.numberView = (TextView)convertView.findViewById(R.id.textView2);
            holder.cb = (CheckBox)convertView.findViewById(R.id.checkBox1);
            convertView.setTag(holder);                
        } else {
            holder = (ViewHolder)convertView.getTag();
        }
        holder.nameView.setText(mData.get(position).toString());
        holder.numberView.setText(mNumber.get(position).toString());
        holder.cb.setChecked(false);
        holder.cb.setTag(position);



        if(selected.indexOf(mNumber.get(position).toString()) >= 0)
        {

        holder.cb.setChecked(true);
        }

        return convertView;
    }

}

这里我在 getView() 上所做的事情我取消选中所有复选框并再次手动检查那些我需要根据检查进行检查的复选框到它对应的文本视图。因此,如果用户在选中第一个复选框后向下滚动,则视图中的所有复选框都将被取消选中,如果他再次向上滚动,那么所有复选框也将被取消选中,但他之前单击的复选框将被再次重新选中。

I was also facing a similar king of problem, so after lot of reading I solved this problem like this:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.listview, null);
            holder = new ViewHolder();
            holder.nameView = (TextView)convertView.findViewById(R.id.textView1);
            holder.numberView = (TextView)convertView.findViewById(R.id.textView2);
            holder.cb = (CheckBox)convertView.findViewById(R.id.checkBox1);
            convertView.setTag(holder);                
        } else {
            holder = (ViewHolder)convertView.getTag();
        }
        holder.nameView.setText(mData.get(position).toString());
        holder.numberView.setText(mNumber.get(position).toString());
        holder.cb.setChecked(false);
        holder.cb.setTag(position);



        if(selected.indexOf(mNumber.get(position).toString()) >= 0)
        {

        holder.cb.setChecked(true);
        }

        return convertView;
    }

}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文