简体   繁体   中英

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

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? 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. which we set in getView of adapter class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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