简体   繁体   English

带有复选框的Android自定义ListView

[英]Android Custom ListView with Checkbox

greetings my fellow developers, i am working on a project in which i have a listview consists on 2 textviews and 1 checkbox in each item, my listview is working as i want it to, except for when i programmatically use setSelection(some_index) on the list view all the chechboxes that were check on previous OnItemClickListener calls gets unchecked, i am trying to figure out why this is but so far no luck, i did try to check and change all the check boxes after each scroll with this 问候我的开发人员,我正在一个项目中,我有一个列表视图,其中每个项目包含2个文本视图和1个复选框,我的列表视图按我想要的方式工作,但当我在列表上以编程方式使用setSelection(some_index)时除外列表视图未选中以前的OnItemClickListener调用上的所有chechbox,我试图找出原因,但到目前为止没有运气,我确实尝试在每次滚动后检查并更改所有复选框。

for (int i = 0; i < listView.getCount(); i++) {
            View mChild = listView.getChildAt(i);
            View mChild1 = listView.getAdapter().getView(i, null, null);
            TextView name = (TextView) mChild.findViewById(R.id.contact_name);
            TextView phone = (TextView) mChild.findViewById(R.id.contact_number);
            CheckBox box = (CheckBox) mChild.findViewById(R.id.contact_check);
            if(contact.getName().equals(name.getText().toString()) && contact.getPhone().equals(phone.getText().toString()))
                box.setChecked(true);
        }

both methods (mchild and mchild1) dont work, plus it is taking to much processing time, since my list view will likely be very long,(give or take 500 items) any help regarding this issue, i have been stuck on this for couple of days, but couldn't comeup with any considerable solutions, one thing i dont understand is that why my listview dataset is changing on scroll, any help, is there is any easier and faster way to do this, then itetrating through childs of listview? 两种方法(mchild和mchild1)都不起作用,而且要花很多时间,因为我的列表视图可能会很长,(给出或占用500个项目)有关此问题的任何帮助,我在这方面一直陷入困境的日子,但无法提出任何可观的解决方案,我不明白的一件事是,为什么我的listview数据集在滚动时发生变化,没有任何帮助,有没有更简便,更快速的方法来执行此操作,然后遍历listview的子元素? if no then why my logic of iteration dont work here is my listview items 如果没有,那为什么我的迭代逻辑在这里不起作用是我的列表视图项

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/list_view_margin_vertical"
        android:layout_marginTop="@dimen/list_view_margin_vertical"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/contact_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/colorBlack"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />

        <TextView
            android:id="@+id/contact_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/colorGrey"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" />
    </LinearLayout>

    <CheckBox
        android:id="@+id/contact_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:layout_gravity="center" />
</LinearLayout>

this is my listview itself 这是我的列表视图本身

<ListView
        android:id="@+id/contactsListView"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:divider="@color/colorAccent"
        android:dividerHeight="1dp"
        android:footerDividersEnabled="false" />

greeting my fello programmers, i figured out there was nothing wrong with the listview or its working, i failed to notice that listview recycles the child views, hense after every recycle default state of the views were loaded, all that is required to fix it is, when getView (adapter class) is calling we are using setText on views to load and view data which is likely coming from database, there is need of a boolean check in the structure of the listview object, and save that boolean to default value, now after each onitem click when we change the checkbox state we must change the boolean check value as well, now the next time getview is called(weather it is calling due to recycling or whatever) we will get the boolean value from the struct and saving as as it is in the checkbox, problem solved, i learned that when dealing with checkboxes or radiobuttons inside listview we must have a boolean check for it. 向我的程序员程序员打招呼,我发现listview或其工作没有问题,我没有注意到listview回收了子视图,在加载了每个回收视图的默认状态后都出现了提示,修复它所需要的是,当调用getView(适配器类)时,我们在视图上使用setText来加载和查看可能来自数据库的数据,因此需要对listview对象的结构进行布尔检查,并将该布尔值保存为默认值,现在每次单击onitem之后,当我们更改复选框状态时,我们也必须更改布尔检查值,现在,下次调用getview(由于回收或其他原因而调用天气)时,我们将从结构中获取布尔值并保存就像在复选框中一样,问题解决了,我了解到,在处理listview中的复选框或单选按钮时,我们必须对其进行布尔检查。 which we set in getView of adapter class 我们在适配器类的getView中设置的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM