如何在可扩展列表视图中保存复选框的状态 - Android

发布于 2024-12-25 18:51:37 字数 226 浏览 1 评论 0原文

我在 Expandable ListView 中显示 SQLite 数据库中的条目,一切正常,但是我想在所有子项旁边添加一个复选框。每次我选中一个复选框,然后向下滚动或收缩我选中的复选框时,它都不会保存该复选框的状态。我知道每当我向下滚动并展开组等时,该列表都会被回收。如何保存每个复选框(每个孩子)的状态?我有一个扩展 CursorTreeAdapter 的类,并且方法中没有“childPosition”。为了拯救国家,我需要做什么?

I'm displaying entries from SQLite database in Expandable ListView and everything is working correctly but, I wanted to add in a checkbox right next to all children. Every time I check a checkbox and then scroll down or contract the checkbox which I had checked it doesn't save the state of the checkbox. I know that the list is recycled whenever I scroll down and expand the groups and so forth. How can I save the state of every checkbox (by every child)? I have a class which extends CursorTreeAdapter and there is no "childPosition" in the methods. What do I need to do in order to save the state?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

瞄了个咪的 2025-01-01 18:51:37

您可以拥有一个布尔标志数组,其大小与列表元素之和相同,并从该数组中为任何组件设置和获取检查值。

you can have an array of boolean flags with same size as sum of list elements and set and get the checked value from this array for any component.

岁月苍老的讽刺 2025-01-01 18:51:37

另一种解决方案是您可以为列表视图中的每个元素保留一个计数器。例如,如果您有 Person 列表,您可以在 Person 类中添加一个计数器变量。

在 getChildView 方法中,将所有代码放入 if 块中,如下所示;

    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, final ViewGroup parent) {

    //FOR SAVE THE STATES!!
    if(parents.get(groupPosition).getCounter()==0) {
        DatePicker datePicker = null;
        Button ok = null;
        Spinner installment = null;
        CheckBox creditCard;
        ......//BLA BLA BLA


        //After finish your codes, increment the counter
        parents.get(groupPosition).setCounter((parents.get(groupPosition).getCounter()+1));
    }

    return convertView;
}

因为对于每个调用的 ExpandGroup 方法,所有初始化(目前这些初始化位于 if 块中)都会重复。因此,你失去了物体的状态。

希望这有帮助。

One more solution is you can hold a counter for each element in list view. For example if you have Person list, you can add one counter variable in your Person class.

And in getChildView method, put all codes in an if block like below;

    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, final ViewGroup parent) {

    //FOR SAVE THE STATES!!
    if(parents.get(groupPosition).getCounter()==0) {
        DatePicker datePicker = null;
        Button ok = null;
        Spinner installment = null;
        CheckBox creditCard;
        ......//BLA BLA BLA


        //After finish your codes, increment the counter
        parents.get(groupPosition).setCounter((parents.get(groupPosition).getCounter()+1));
    }

    return convertView;
}

Because for each expandGroup method called, all the initializations (for now those are in if block) repeats. Because of that you loose your state of objects.

Hope this help.

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